• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1608
  • Last Modified:

How can I parse an xml file that contains multiple root elements using the Java SE 6 StAX parser?

I have a test.xml file that contains the following contents:

<?xml version="1.0" encoding="UTF-8" ?>
<location>
<country>USA</country>
<state>California</California>
<city>Los Angeles</city>
</location>
<?xml version="1.0" encoding="UTF-8" ?>
<location>
<country>USA</country>
<state>Texas</California>
<city>Dallas</city>
</location>

I want to parse it using  the Java SE 6 StAX parser. Please note that the file contains multiple root elements. Is this possible? I only know how to do it by reading each line and then tokenizing the parsed string. However, I need to do this using the StAX parser.
test.xml
-------------------
<?xml version="1.0" encoding="UTF-8" ?>
<location>
<country>USA</country>
<state>California</California>
<city>Los Angeles</city>
</location>
<?xml version="1.0" encoding="UTF-8" ?>
<location>
<country>USA</country>
<state>Texas</California>
<city>Dallas</city>
</location>

Open in new window

0
WarBabies
Asked:
WarBabies
  • 4
  • 2
1 Solution
 
CEHJCommented:
>> Is this possible?

In a word: no ;-) It would be invalid. You need to separate the sections
0
 
Thomas4019Commented:
Coulnt you just add a element at the top and bottom like this. Then there would only be one root element.
test.xml
-------------------
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<location>
<country>USA</country>
<state>California</California>
<city>Los Angeles</city>
</location>
<?xml version="1.0" encoding="UTF-8" ?>
<location>
<country>USA</country>
<state>Texas</California>
<city>Dallas</city>
</location>
</root>

Open in new window

0
 
CEHJCommented:
>>Coulnt you just add a element at the top and bottom like this

You could if you took out all but one xml declaration too
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
WarBabiesAuthor Commented:
Thomas4019, CEHJ is right. However, I'm looking for a creative solution. I cannot change the format of the data because that's what the SW that I'm interfacing with is generating and I have no control over it. However, I'm thinking there has to be a way to read ahead and remove the things that make the content invalid so the rest can be parsed with the StAX parser. Again, I'm looking for a creative solution rather than just reading the file's contents line by line and then tokenizing the read lines.
0
 
CEHJCommented:
>>that's what the SW that I'm interfacing with is generating

Unfortunately it's generating invalid xml. You don't need to change the format of the original - you just need to produce a source from it that *is* valid
0
 
WarBabiesAuthor Commented:
Yes, I agree that there's no direct way to do this with the Java SE 6 StAX parser. However, I also don't have control of the fact that the SW that I'm interfacing with is streaming invalid XML. It's also not time efficient to generate a valid source from the invalid XML data. I was hoping there was a creative, out-of-the-box solution. In either case, I'll keep parsing the XML data line by line and then tokenizing the parsed string.  
0
 
CEHJCommented:
:-)

Personally i would generate a buffer event every time you've read a 'document' from the one file. You can then create a stream on that buffer, which your stax parser can consume
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now