XML解析:使用消解器检索xml中的多行
问题描述:
解析如下所示的xml文件时,我想要获取某个特定id的电话号码列表。我正在使用Digester来完成此操作。但我不理解如何添加调用方法或createobjects。可有人帮我this.My XML文件包含 类型XML解析:使用消解器检索xml中的多行
<?xml version='1.0' encoding='utf-8'?>
<address-book>
<contact type="individual">
<id>50</id>
<city>New York</city>
<province>NY</province>
<postalcode>10013</postalcode>
<country>USA</country>
<address>
<telephone>1-212-345-6789</telephone>
<telephone>1-212-345-6789</telephone>
<telephone>1-212-345-6789</telephone>
<telephone>1-212-345-6789</telephone>
</address>
</contact>
<contact type="business">
<id>52</id>
<city>Zagreb</city>
<province></province>
<postalcode>10000</postalcode>
<country>Croatia</country>
<address>
<telephone>1-212-345-6789</telephone>
<telephone>1-212-345-6789</telephone>
<telephone>1-212-345-6789</telephone>
<telephone>1-212-345-6789</telephone>
</address>
</contact>
此外,我应该如何停止解析当我得到所需的ID为1000的。
答
虽然问题是使用apache-commons-digester的具体问题,但这可以通过XML函数族中已有的库的主机来解决 - 即SAX解析器与XPath搜索相结合。 XPath查询可以相对有效地找到数据,而不是通过数据强行进行搜索,而是知道正在搜索的内容。否则,如果为了索引或其他目的遍历整个数据集,再次推荐使用简单的SAX解析器并遍历元素(可能通过// MyElement类型的XPath查询),然后为每个实例传递值一个索引或任何操作的函数。 apache-commons-digester可能过于复杂和/或需要什么缓慢。