Java:如何使用 SAX 确定 XML 解析期间的深度级别
我正在扩展 org.xml.sax.helpers.DefaultHandler 来解析 XML。 在解析过程中如何确定深度级别?
例如:
<?xml version="1.0" encoding="utf-8"?>
<jsk:Dataset gml:id="Dataset1" ....>
<gml:description>....</gml:description>
<gml:boundedBy>
...
</gml:boundedBy>
</jsk:Dataset>
标签位于级别 0
和
标签位于级别 0在第一级等等...
任何关于正确方向的指导都值得赞赏。
I am extending org.xml.sax.helpers.DefaultHandler to parse a XML.
How can you determine the depth level during parsing?
for example:
<?xml version="1.0" encoding="utf-8"?>
<jsk:Dataset gml:id="Dataset1" ....>
<gml:description>....</gml:description>
<gml:boundedBy>
...
</gml:boundedBy>
</jsk:Dataset>
<jsk:Dataset>
tag is on level 0
<gml:description>
and <gml:boundedBy>
tags are on level 1 and so on...
any guidance on the right direction is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DefaultHandler 类指示它正在通过
startElement
方法处理新元素,并且已使用endElement
方法完成对新元素的处理。您可以通过在子类中覆盖这些方法来连接它们。使用实例字段存储状态的另一个答案中所述的方法也可以在这种情况下使用。输入
startElement
时递增,退出endElement
时递减。The DefaultHandler class indicates that it is processing a new element via the
startElement
method, and that it has finished processing the same using theendElement
method. You can hook onto these methods by overriding them in your child class.The approach stated in the other answer of using an instance field to store state can be used in this case as well. Increment on entering
startElement
and decrement on exitingendElement
.每次调用
SAXListener.startTag()
时,深度都会增加。每次调用
SAXListener.endTag()
时,深度都会减少。在这些回调方法中递增/递减处理程序类的实例字段以跟踪您的深度。
Every time
SAXListener.startTag()
is called, the depth is increasing.Every time
SAXListener.endTag()
is called, the depth is decreasing.increment/decrement an instance field of your handler class in these callback methods to keep track of how deep you are.