【发布时间】:2017-05-08 21:49:44
【问题描述】:
我正在解析 Java 应用程序中的自定义 XML 配置文件。我正在尝试使用SAX解析器,主要是因为我需要用行号报告配置中的错误。
网上有很多实现处理程序类的代码示例,对于正常处理来说,事情似乎相当简单——例如,http://tutorials.jenkov.com/java-xml/sax-example.html
但就我而言,有时我需要跳过一个元素下的整个树:
<sampledocument>
<sampletag>
<process/>
<these/>
<tags/>
</sampletag>
<sampletag skip="yes">
<do_not>
<process/>
<these/>
<tags/>
</sampletag>
<sampledocument>
稍后添加:此外,我只知道是否在运行时跳过。在一个有点做作的例子中,我需要打开一个文件来处理<sampletag> 下的标签,如果找不到该文件,则不处理它们:
<sampledocument>
<sampletag file="file1">
<process/>
<these/>
<tags/>
<if_file1_exists/>
</sampletag>
<sampletag file="file2">
<process/>
<these/>
<tags/>
<if_file2_exists/>
</sampletag>
<sampledocument>
当然,我可以在处理程序代码中跟踪跳过,但这有点尴尬。我可以在 startElement() 方法中以某种方式告诉 SAX 跳过该元素的内容吗?
【问题讨论】: