Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Stax - Duplicate named elements

 
Tom Swe
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20954
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20954
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 656
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic