wood burning stoves 2.0*
The moose likes Java in General and the fly likes Using XMLStreamReader to return actual XML strings of children? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using XMLStreamReader to return actual XML strings of children?" Watch "Using XMLStreamReader to return actual XML strings of children?" New topic
Author

Using XMLStreamReader to return actual XML strings of children?

Jeff Grant
Ranch Hand

Joined: Dec 19, 2001
Posts: 169
I am attempting to use the javax.xml.stream.XMLStreamReader class to read an XML data file. I have it working correctly and giving all requested results. However, I would like to parse out certain subsections as XML without having to reconstruct it manually to then parse it elsewhere.

Here is an example.



From this example, I might want to retrieve all of "section2" to another class I've built to hold the nodes to then later reparse. I need it to retain it's XML format so I can continue using XMLStreamReader to parse it later on.

First question is if it's possible to get sub-elements actual XML rather than parsing based on local name and element text?

Section question is if this is possible to parse by storing in an array rather than reading from a file?.. if not, I will probably generate a temporary file to hold this retrieved information and then reparse it.

Thanks in advance for any suggestions. I am open to using another class if it is freeware and can do this requested task.

-Jeff

P.S. I am surprised at the lack of XMLStreamReader posts on JavaRanch! I only found one when searching all of JavaRanch via the google search feature.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18129
    
    8

I am not surprised at the lack of posts. I had never heard of it, so I went looking for it. Not in the standard Java API, not even in Java 5. I googled a bit and found it in JSR 173, a.k.a. StAX.

I know basically nothing about StAX except a one-line description of what it does, so let me answer your questions based on general principles of how XML things work.
First question is if it's possible to get sub-elements actual XML rather than parsing based on local name and element text?
You mean you want to leave parts of the document unparsed? Doubt it. Parsers don't work that way. They convert XML to internal formats. You would have to serialize those bits back to XML if you wanted them that way.
Section question is if this is possible to parse by storing in an array rather than reading from a file?.. if not, I will probably generate a temporary file to hold this retrieved information and then reparse it.
"Storing in an array" versus "Reading from a file"? I don't see any contradiction or overlap between those two concepts. You can parse from pretty much anywhere, and you can store the data wherever you like. Maybe you mistyped something there?
Jeff Grant
Ranch Hand

Joined: Dec 19, 2001
Posts: 169
Thanks for looking into this question for me since you had never heard of the class.
You mean you want to leave parts of the document unparsed? Doubt it. Parsers don't work that way. They convert XML to internal formats. You would have to serialize those bits back to XML if you wanted them that way.

Yes, that's what I'm looking for. The purpose is to parse out all data elements and local names from within a "transaction" set for another function to deal with the transaction itself to avoid cluttering a single "transaction getter" function.

I understand what you're saying though how that'd defeat the purpose of a parser.
"Storing in an array" versus "Reading from a file"? I don't see any contradiction or overlap between those two concepts. You can parse from pretty much anywhere, and you can store the data wherever you like. Maybe you mistyped something there?

Yeah, I definitely worded that poorly.

I have continued working on this issue this morning and found exactly what you said. I stored some XML in a string and read that into XMLStreamReader instead of reading from the file and it worked as was stored. So rather than a temporary file, I will likely store the XML into a string to save time.

So now here is where I am at since XML strings themselves seemingly cannot be pulled from the XMLStreamReader class. XML is plain text and the format of my XML file format is consistent, I am now thinking I will read it via FileInputStream and just look for the beginning and ending tags saving every line in between storing it into a single string as described above. Then I will be able to parse it with the XMLStreamReader in the next function to process the set.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I've seen something like this before using a SAX parser to read through the XML, ignore and discard a SOAP envelope and write the payload parts back out as XML. It was rather icky.

Are you able to change or replace the object that expects an XML stream starting with <section2> ?? If I had any choice in the matter I'd switch gears to SAX parsing with a nested Reusable SAX Parser scheme.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jeff Grant
Ranch Hand

Joined: Dec 19, 2001
Posts: 169
That looks like a possibility as well Stan. Thanks for the suggestion.
KR Campbell
Ranch Hand

Joined: Mar 26, 2004
Posts: 124
Apologies if I have not properly picked up the gist of your problem, but, have you considered binding your xml using either JAXB or XmlBeans? You'll need to learn to write an XML Schema for your source xml file but then you would be able to generate classes and use an XmlCursor or similar to parse out your data using XPath expressions.

I've also used XSLT for this in conjunction with the javax.xml.transform package and then passed the result through a pipe (this approach is relatively slow with large documents).

Regards,
Ken
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using XMLStreamReader to return actual XML strings of children?
 
Similar Threads
Form Level Error Messages In JSF
writing java code that will take data from certian xml <elements> and convert into tree nodes
Unable to write back the content of XML
how to control the html layout according to the input data within XSL
Another CSS/IE conundrum