开发者

Trouble with SAX Parser

I'm having an xml file in the below format:

<Query>
   <map>
      <value name="val1">1</value>
      <value name="val2">2</value>
   </map>
</Query>
<Quest>
   <map>
      <value name="val1">6</value>
      <value name="val2">8</value>
   </map>
</Quest>

When I write my SAX parser, I get all the values from start to end, but I need to write a condition what would take in a tag name, like Query / Quest and get only the name and value for their specific.

I'm not sure how to add this condition, I don't need to parse the rest of the tags once my condition is met and my object is 开发者_运维知识库parsed.

NOTE: I'm writing in Java.


  • In startElement, check the element name against the given name. For example, if you are looking for "Query" and the name is "Query", set some flag to true.
  • In startElement, if the element name is "value" and the flag is true, store the name and value in a map.
  • In endElement, if the element name equals "Query", set the flag to false. Optionally, stop parsing altogether.


If you only want the data for a particular set of elements then a DOM based parser especially one with XPath to get the required fields might be easier.

An xpath library is jaxen which can be used with several doms I have used JDOM

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜