We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Copying the data from one XMLElement to another
Page Index Toggle Pages: 1
Copying the data from one XMLElement to another (Read 601 times)
Copying the data from one XMLElement to another
Nov 5th, 2008, 4:51pm
 
I need to copy all data from one XMLElement to another without destroying the reference to the target XMLElement.

So this won't do for me:
Quote:
XMLElement targetXMLElement = new XMLElement();
targetXMLElement = sourceXMLElement;


I can do it like that:
Quote:
XMLElement targetXMLElement = new XMLElement();
targetXMLElement.addChild(sourceXMLElement);

but then
Quote:
println(targetXMLElement);

will print "null", although
Quote:
println(targetXMLElement.getChild(0));

prints all data correctly.

Is there a better way to do this or is this a bug?


------
Processing 0155
Re: Copying the data from one XMLElement to anothe
Reply #1 - Nov 7th, 2008, 9:59pm
 
Using what library (I don't see an addChild in XMLElement which seems able only to read XML, not to manipulate Dom.)

In JavaScript, I would use cloneNode method to duplicate a node.
Re: Copying the data from one XMLElement to anothe
Reply #2 - Nov 7th, 2008, 10:34pm
 
I didn't use a library. The reference doesn't seem to be complete yet. I found the addChild method in the Developers Reference (http://dev.processing.org/reference/core/index.html).

I'm just wondering, why an XMLElement that obviously has one child node still can be null ..
Re: Copying the data from one XMLElement to anothe
Reply #3 - Nov 8th, 2008, 12:14am
 
Aha! You are right... That's NanoXML 2, to be precise.

Your method is incorrect, anyway. According to reference, new XMLElement() creates an empty element, without tag name. It is not null, otherwise addChild and getChild would throw an exception. But its toString() method, using XMLWriter manages to display null, because it is an empty shell.

When you do addChild, you put the element inside the empty element. But that doesn't make it a full, unique XML element... That's an incomplete compound (nested) element.
And I don't know this library, and the JavaDoc is quite terse, but if addChild works like JavaScript's appendChild, it actually moves the source element from its initial position to the given one.

If the source element is simple, perhaps you can clone it by doing:

XMLElement targetXMLElement = new XMLElement(sourceXMLElement.toString());

No guarantees... Smiley
Page Index Toggle Pages: 1