I have an XML file that I am trying to parse with Sax (this is my first time doing this). I’ve researched how to implement the parser, and that all makes sense, but I’m unsure of the best way to tackle this problem.
I have scattered pieces of related data (two Facts are associated by a FactKey). In the example below, Foo has a value of 5.34.
Sax calls StartElement() on every new element, so that is one call for Facts and one call for Value….so my questions are: Do I have to store FactKey from the Facts element so I can associate it with the Value element on the next pass, or is there a way for Sax to do that automatically?
And is there any facility built in for associating two different Facts with the same FactKey, perhaps if I used DOM instead of Sax? Or is that just wishful thinking, and I actually just need to maintain a multimap or something.
...
<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>
For your first question: yes, you have to maintain any context that is used by the parser (ie, you have to keep track of the fact that you are in/not-in a
Factselement).As for associating different
Factelements by key, yes, with caveats. You can load the file into DOM (assuming that you have enough memory), then use XPath to extract all elements with a specific FactKey.However, if you want to read the file and accumulate
Factswith the same key, then a multimap is your best bet. A DOM parser may still be useful, as you could have a multimap that associates the string keys toElementvalues.