Page 1 of 1

E4X setNamespace not working

PostPosted: Thu Oct 06, 2011 2:43 pm
by Ruben79
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?

Re: E4X setNamespace not working

PostPosted: Thu Oct 06, 2011 3:20 pm
by Ruben79
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...