Copy link to clipboard
Copied
I'm trying to create an XML object and add children to it. I can create the object, add one descendant per descendant, but when I try and add more that one child at the same level, they all get placed in the same node. Here's a little test I did:
var myXML = new XML()
myXML = XML('<rootnode></rootnode>') //this created the root node fine.
myXML.firstChild[0] = "Dave" //This creates a child node fine with the contents "Dave"
myXML.firstChild[1] = "Sam" //This adds Sam to the node Dave is in rather than creating a new child.
myXML.firstChild[0].secondChild = "Betty" // This creates a child node fine under the first child.
myXML.firstChild[1].secondChild = "Betty" //this will generate an error as there is not a second child.
Using insertChildAfter or insertChildBefore or appendChild don't seem to do anything either.
Any ideas on what I'm doing wrong?
Copy link to clipboard
Copied
Looks like I have to use:
var newChild = new XML('<firstChild>' + someVar + '</firstChild>')
myXML.appendChild(newChild).
Just seems you should be able to do it using E4X syntax.
Copy link to clipboard
Copied
Firefox behaves the way you expect:
<rootnode>
<firstChild>
Dave
<secondChild>Betty</secondChild>
</firstChild>
<firstChild>
Sam
<secondChild>Betty</secondChild>
</firstChild>
</rootnode>
I would consider this a bug.
-X
Copy link to clipboard
Copied
Thanks, Xbytor. That's what I was thinking, but I don't know enough about this to say for sure.
Chuck