开发者

How to use SAX on this xml file

开发者 https://www.devze.com 2023-04-07 11:10 出处:网络
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 tac

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=开发者_如何学Go"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>


You can use SAX to do this sort of thing, but you will probably find it gets tedious quickly. SAX is a basic building block sort of tool. Assuming your documents are less than 20 MB or so, you will almost certainly find it more convenient to load the entire document in memory and process it using more powerful tools. DOM is a bit tedious to program against too, mostly because its API is poorly designed, but has the virtue that you can run XPath expressions against it, effectively letting you find all nodes with a certain key value. You might find that other tree APIs like JDOM, XOM and DOM4J are more to your liking. However ultimately you will probably end up wanting to use a richer programming language like XSLT or xquery. XSLT has a built in "key" directive that will let you define an index for rapid lookup of items based on keys like those you describe, and provides a rich programming environment for processing XML.


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 Facts element).

As for associating different Fact elements 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.

//Facts[@FactKey="2832154551"]

However, if you want to read the file and accumulate Facts with 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 to Element values.


I've used dom and from just reading up about sax I don't think that either can do what you're asking.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号