How to use SAX in this xml file

I have an XML file that I am trying to parse with Sax (this is my first time doing this). I researched how to implement a parser, and that everything makes sense, but I'm not sure how best to solve this problem.

I scattered pieces of related data (two facts are related to FactKey). In the example below, Foo has a value of 5.34.

Sax calls StartElement () for each new element, so this is one fact call and one value call .... so my questions is: I need to store the FactKey from the Facts element so that I can associate it with the Value element in the next pass, or is there any way for Sax to do this automatically?

And is there any way to combine two different facts with the same FactKey, perhaps if I used the DOM instead of Sax? Or is it just wishful thinking, and in fact I just need to support a multimap or something like that.

... <Facts FactKey="2832154551" FieldId="73250"> <Value xsi:type="xs:double">5.3499999</Value> </Facts> ... <Facts FactKey="2832154551" FieldId="410288"> <Value xsi:type="xs:string">Foo</Value> </Facts> 
+4
source share
3 answers

For your first question: yes, you should support any context that is used by the parser (i.e., you should ensure that you are in / not in the Facts element).

As for comparing various Fact elements with a clue, yes, with reservations. You can load the file into the DOM (assuming you have enough memory), and then use XPath to extract all the elements using a specific FactKey.

 //Facts[@FactKey="2832154551"] 

However, if you want to read the file and copy Facts with the same key, then you are best off using multimap. The DOM parser may still be useful, as you may have a multimap that associates string keys with Element values.

+1
source

You can use SAX for this kind of thing, but you will probably find that it got tired quickly. SAX is the main tool for the building block. Assuming your documents are less than 20 MB, you will almost certainly find it more convenient to load the entire document into memory and process it using more powerful tools. The DOM is a bit tedious for programming against, mainly because its API is poorly designed, but has the advantage that you can run XPath expressions against it, effectively allowing you to find all nodes with a specific key value. You may find that other tree APIs, such as JDOM, XOM, and DOM4J, are to your liking. However, in the end, you probably end up wanting to use a richer programming language such as XSLT or xquery. XSLT has a built-in โ€œkeyโ€ directive that allows you to define an index to quickly search for items based on keys such as those you describe, and provides a rich programming environment for processing XML.

+3
source

I used dom and just read about saxophone. I do not think that either can do what you ask.

0
source

All Articles