• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Stax - Duplicate named elements

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Space pants. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic