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 Stax - Duplicate named elements 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 "Stax - Duplicate named elements " Watch "Stax - Duplicate named elements " New topic
Author

Stax - Duplicate named elements

Tom Swe
Greenhorn

Joined: Jun 14, 2011
Posts: 5
Hello, i need a little guidance please. I have an XML file, which i am reading with the Stax API, all is working fine using the EventReader. My problem is that within the parent element there exists child elements and within those child elements exist elements that sometimes have the same names.... See this sample for an idea of what i mean.....



<advisory>
<name>XXXXXXXXXX</name>
<testDescription>XXXXXXXXXXXX</testDescription>
<threat>
<name>XXXXXXXXXXX</name>
<reference>XXXXXXXXXXXXXXXX</reference>
</threat>
............
............
</advisory>


What happens is when i get the element <name> it will always cycle through both <name> elements an print me out the last one. I want it so that i can get the <name> element that is relative. I.e. the <name> element for <advisory> and then the <name> element for <threat>.

Do you know how i can distinguish between the 2??

Thanks in advance.

Tom
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18140
    
    8

The short answer is that your code has to keep track of where it is in the document. In other words, it needs to know whether it's inside a <threat> element or not.
Tom Swe
Greenhorn

Joined: Jun 14, 2011
Posts: 5
Paul Clapham wrote:The short answer is that your code has to keep track of where it is in the document. In other words, it needs to know whether it's inside a <threat> element or not.


Hey, first and foremost thanks for your response.

Being as though i'm new to StaX and for that matter Java, would you be able to maybe point me in the direction to find out more on how i will keep track of the position???

Thanks

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18140
    
    8

Since you're using Stax parsers I have to assume you already know about boolean variables.

So use one. Set it to "true" when you read the <threat> start tag and set it to "false" when you read the matching end tag. Then you can inspect that variable to see if you're inside a <threat> element or not.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 464
    
    2
You can use a Stack to keep track of the whole ancestor axis for each context element. In particular, it would be suitable for a quick peek of what the parent element is.

Your change to the whole layout would probably look like this.

That is about it.
Tom Swe
Greenhorn

Joined: Jun 14, 2011
Posts: 5
Hello,

Thanks to the both of you for aiding me in my resolve of this little problem.

I resolved it by using a couple of Boolean variables, one for Advisory and one for Threat, if the parser had reached them they would return true or not, and some code would be ran etc...

Thanks to Paul for the suggestion, also thanks to G tsuji. I haven't tried your suggestion but i will bare it in mind in the future.

Cheers

Tom
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stax - Duplicate named elements
 
Similar Threads
DTD
XML Certification Notes
Attribute declaration in XML Schema
How to delete NameSpace for a sub child of org.jdom.Element?
How to get nested elements using DOM