E4X setNamespace not working

Questions, tips and tricks and techniques for scripting in Servoy

E4X setNamespace not working

Postby Ruben79 » Thu Oct 06, 2011 2:43 pm

I am trying to generate an XML using E4x in Servoy 5.2.4.
Code: Select all
var xml = <root><foo/></root>;
var ns = new Namespace('ns','http://example.org/');
   
xml.addNamespace(ns);
xml.foo = 'bar'; 

application.output(xml.toXMLString());

Until this, the code works fine. But when I actually want to use the namespace in the <foo/> node, using:
Code: Select all
var xml = <root><foo/></root>;
var ns = new Namespace('ns','http://example.org/');
   
xml.addNamespace(ns);
xml.foo = 'bar'; 
       
xml.foo.setNamespace(ns);
   
application.output(xml.toXMLString());

It throws the following error:
Code: Select all
NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.
> org.w3c.dom.DOMException: NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.

How is this possible? As far as I can see on the internet, this should be the way to do it. Or am I missing something?

I can get it to work using this code:
Code: Select all
xml.ns::foo = 'bar';

But I need to do it for multiple items in a loop so this approach won't work as it will overwrite the existing node. Anyone have experience with this?
Ruben de Jong
Stb Software Development
SAN Partner

Stb Software Development - http://www.stb.nl
User avatar
Ruben79
 
Posts: 97
Joined: Wed Apr 18, 2007 12:43 pm

Re: E4X setNamespace not working

Postby Ruben79 » Thu Oct 06, 2011 3:20 pm

Hmm, I can get one step further using:
Code: Select all
xml.foo.setNamespace([ns]);


But the output is not what it needs to be:
Code: Select all
<root xmlns:e4x_0="[http://example.org/]">
  <e4x_0:foo>bar</e4x_0:foo>
</root>


Seems that it cannot find the prefix...
Ruben de Jong
Stb Software Development
SAN Partner

Stb Software Development - http://www.stb.nl
User avatar
Ruben79
 
Posts: 97
Joined: Wed Apr 18, 2007 12:43 pm


Return to Methods

Who is online

Users browsing this forum: No registered users and 9 guests

cron