Create XML representaion of class instance from down to up?

The Cadet

New Member
I was thinking to write a method in \[code\]toString()\[/code\] like fashion so that it return XML representation of the class instance.First I was thinking to write it like\[code\]public Element toElement() { // create Element instance and fill it}\[/code\]But I was unable to create empty \[code\]Element\[/code\] instance inside, since \[code\]Element\[/code\] creation requires \[code\]Document\[/code\] instance to call it's \[code\]createElement()\[/code\]. So I rewrote method to\[code\]public Element toElement(Document doc) { Element ans = doc.createElement("myclasstag"); // filling ans return ans;}\[/code\]But then I got runtime exception \[code\]HIERARCHY_REQUEST_ERR\[/code\] since one can't fill \[code\]Element\[/code\] instance until it is attached to parent hierarchy.So I was to rewrite method as follows \[code\]public Element toElement(Document doc, Element parent) { Element ans = doc.createElement("myclasstag"); parent.appendChild(ans); // filling ans return ans;}\[/code\]But this way I need not return ans since it is already attached where it should be, so it became\[code\]public void append(Document doc, Element parent) { Element ans = doc.createElement("myclasstag"); parent.appendChild(ans); // filling ans}\[/code\]which is now absolutely dislike \[code\]toString()\[/code\].Is it possible to create XML instance from down to up fashion like \[code\]toString()\[/code\] does?
 
Back
Top