extract inner xml...

L

Loocek

Hi,

I need to find and extract inner xml and save as String.
I cannot find that functionality in any [DOM,SAX,STAX] xml parser in java...
I was able to do it in DOM by serializing Node - but is stupid solution....
Would you give me any suggestion [or part of example] how to do it?
My XMLs are quite big... [100k]

Thanks for any help

Regards
Pawel Z
 
M

Martin Honnen

Loocek said:
I need to find and extract inner xml and save as String.
I cannot find that functionality in any [DOM,SAX,STAX] xml parser in
java...
I was able to do it in DOM by serializing Node - but is stupid solution....

I am not sure why it is a stupid solution. If you want the inner XML
then you will have to serialize the child nodes, unless the object model
is not already providing properties like InnerXml (as for instance the
..NET framework's DOM model does:
http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.innerxml.aspx)

If you are using the W3C DOM then Level 3 has serialization features
http://www.w3.org/TR/DOM-Level-3-LS/
I think Java 5 and 6 implement that. In the browser world of the DOM
only Opera supports it but Mozilla has XmlSerializer and IE/MSXML have
the xml property on XML DOM nodes.
 
L

Loocek

Hi again,

Martin said:
Loocek said:
I need to find and extract inner xml and save as String.
I cannot find that functionality in any [DOM,SAX,STAX] xml parser in
java...
I was able to do it in DOM by serializing Node - but is stupid
solution....

I am not sure why it is a stupid solution.
A "raw" input stream of data is read by parser. It creates objects in
memory, next base on the object - we create a new "raw" stream that is
exactly substring of original string. It waste memory and cpu....


If you want the inner XML
then you will have to serialize the child nodes, unless the object model
is not already providing properties like InnerXml (as for instance the
.NET framework's DOM model does:
http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.innerxml.aspx)
I know it is simple. One method call ...
If you are using the W3C DOM then Level 3 has serialization features
http://www.w3.org/TR/DOM-Level-3-LS/
I think Java 5 and 6 implement that. In the browser world of the DOM
only Opera supports it but Mozilla has XmlSerializer and IE/MSXML have
the xml property on XML DOM nodes.


Thanks for fast answer.
Regards
Pawel Zawora
 
R

Richard Tobin

A "raw" input stream of data is read by parser. It creates objects in
memory, next base on the object - we create a new "raw" stream that is
exactly substring of original string. It waste memory and cpu....

Well, sometimes. Sometimes the DOM is modified after it's read in.
Even if it isn't, perhaps it needs namespace declarations that were
not present in that fragment originally. Hanging fragments of the
original text on to the relevant parts of the DOM just in case it gets
used is a waste of memory and cpu 99.9% of the time.

Making your case slow and the usual case fast is probably better
overall.

-- Richard
 
A

Albert

Loocek a écrit :
Hi,

I need to find and extract inner xml and save as String.
I cannot find that functionality in any [DOM,SAX,STAX] xml parser in
java...
I was able to do it in DOM by serializing Node - but is stupid solution....
Would you give me any suggestion [or part of example] how to do it?
My XMLs are quite big... [100k]

Thanks for any help

Regards
Pawel Z

You can do it in SAX by enabling a flag when the "inner" XML start, and
then keep reconstructing the XML from XML events (start/endElement() and
characters() mainly) until the end of the inner XML is reached.

HTH
 
Joined
Nov 28, 2009
Messages
1
Reaction score
0
Use VTD-XML

With VTD-XML you can use getElementFragment() to extract inner XML in one function call, in a byte array, it is the most elegant solution
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,843
Latest member
WizcraftEntertainmentAgen

Latest Threads

Top