File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes copy xml string into a dom tree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "copy xml string into a dom tree" Watch "copy xml string into a dom tree" New topic
Author

copy xml string into a dom tree

aswin pal
Greenhorn

Joined: Jan 23, 2010
Posts: 9
I have a DOM tree, and a node in it into which I need to copy another XML node.

The general examples advise to load the source as one more DOM tree, do a importNode.

The problem is performance.

I have a huge string (depth-wise) and am looking for alternatives.

Any ideas?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

What exactly were the performance problems which you encountered?

And what exactly are you looking for alternatives to? Do you want to scrap the idea of using a DOM and rewriting the application to handle the XML some other way? Or were you just looking for some other way to import data into an existing DOM?
aswin pal
Greenhorn

Joined: Jan 23, 2010
Posts: 9
The problem is that I have a DOM tree into which I want to copy XML from a file. That XML content is huge in the number of nodes and their levels.

Loading it into a DOM node and using importNode will be expensive in my case, and I was just trying to see if I can copy that entire thing just as a String into my DOM tree.

Existing DOM tree in code:

<a>
<b>[insert huge XML here]</b>
</a>

XML:

<x>
<y>
<z>
</z>
</y>
</x>

Anish Kuti
Greenhorn

Joined: May 12, 2008
Posts: 29
From your reply it seems .. the problem is huge XML - that memory consumption is high.
You can try using XML bean for this.
aswin pal
Greenhorn

Joined: Jan 23, 2010
Posts: 9
Thanks. Could you tell the high-level steps.

Download http://xmlbeans.apache.org/ and...
aswin pal
Greenhorn

Joined: Jan 23, 2010
Posts: 9
Also, a quick read of http://xmlbeans.apache.org/ tells me of it as a java-xml binding api.

My XML is without a schema and all I want is to add huge XML to existing DOM tree without importNode that will parse that content (and therefore consume time).
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12792
    
    5
Why are you using DOM at all, why not treat the problem as one of merging two text streams?

If you absolutely need to parse document 1 to find the insertion point, do it with SAX, writing the contents of all events as Strings to a new output file and inserting your new text when you hit the insertion point.

Bill
aswin pal
Greenhorn

Joined: Jan 23, 2010
Posts: 9
Worked. thanks. was over-engineering and forgot about good old SAX.
aswin pal
Greenhorn

Joined: Jan 23, 2010
Posts: 9
Also, would passing xml to createTextNode() as a normal string be a bad idea? I have not tried it at the moment...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

aswin pal wrote:Also, would passing xml to createTextNode() as a normal string be a bad idea? I have not tried it at the moment...

It would be a bad idea if you expected the createTextNode method to parse the XML into nodes. It would be a good idea if you wanted the XML to remain as an uninterpreted string, and if you expected the markup (characters like < and >) to be escaped.
 
GeeCON Prague 2014
 
subject: copy xml string into a dom tree