for example I would like to parse an XML document which is like this:
<testDescription>nom de test</testDescription>
like a remark the balise <name> is contained in parent <advisory> and <threat> So I should detect that to put them in respective objects. So wen I googled, I found that I can use Stack (peek, pop, ...) with staX to resolve this problem, but I can't find an example.
Please if you found an example or tuto send me the link.
By far the easiest way to look at XML document structure is to parse the document to a DOM and use the org.w3c.dom package classes and methods.
In saying "by far" I mean 10 or 100 times easier. The reason being that DOM knows about the parent/child relationships.
Only in the event of really really huge input documents would you require event oriented parsing.
Harold's free online book will have examples.
I'd like to use Stax with the fonction of "peek" and "pop" to resolve duplicate balise name in XML.
I wonder where do you come up with peek and pop if not put to you by your instructors or something?! They are referring to peek and pop of some instance of a Stack.
At the same time of running a XMLEventReader, you maintain a stack of QName: pushing element name when it has seen a StartElement and popping the element name at the head when it has seen an EndElement. But you decide to push the element name to the stack only after "peeking" its last-in entry (see below) so as to ascertain the current element's parent name.
When the event reader is at a StartElement, when its name is "name", you have the stack "peek" its entry at the top (last-in entry). If it is either advisory or threat, you know you find one searched element "name" who parent is either "advisory" or "threat". And then you do whatever desirable to the name tag so found.
That is the idea. Try to implement it yourself (show your effort). If you find difficulties in the implementation of it, I may or somebody may help you further if so desired and satisfied you've done your due.
My Java Class:
it should be like this ? am I right ??
it should be like this ? am I right ??
I take it that the question is not directed to me but to others who answered to this thread, as that is not what I was saying in answering the original question (and more importantly the question being asked in the original context...) so I feel free to pass by only saying that it can be like that and it can be other than that. It depends on how the question is being asked and why it is asked that way. If you feel happy to code this way, why not?
The merit of the question asked in the original way (by your instructors) is that as StAX parsing is forward-only, how would you embrace this characteristic in case when a looking backward is needed locally to fulfill specific functionality. That is the generic meaning of the question.
But if you know every single detail of the document beforehand: which tag (name and kind) following which, then why not code like that in a rigid way, sacrificing flexibility which is not the purpose of it after all.
I don't find any sample code that which contain that, so i demand a link or a small sample. But I don't have an issue right now. So I develop like I see it. I know it's not a good method.
So please if you have an idea how to correct it tell me please.
my teacher tell me to develop this class to parse XML File by using StaX and I find many cases of duplicate tags that's not have the same parents.
Very important point here.
XML cares about the structure of a document so you are not finding "duplicate tags" you are finding Elements with duplicate names. Duplicate names may appear in perfectly valid XML documents - a very bad design to be sure but still valid XML.
I find many cases of duplicate tags that's not have the same parents.
If you want to work with xml, be sure not to develop prematurely any wrong opinion around this phenomenon (yes, duplicate tag name as said) in particular at this early stage when you may not really understand that much in the xml and the technologies developed around it in practice. It is perfectly normal. Is it bad design? Arguably... I find it hard to argument in the other direction, that's for sure. But I certainly won't even say it is bad design. It may cause some inconvenience to deal with it at time. If you have luxury to sit back and just do your own work, no reason to make inconvenience for yourself on a work of your own.
Since you want to deal with it somehow, I can show you how in the line of reasoning I had made explicit. Let me take your second xml. Let me take "code" tags. It may appear as child of "values" tag or as child of "materiel" tag. This is one way to get the info.