有没有办法通过SAX / DOM解析XML,每个节点可以使用行号 [英] Is there a way to parse XML via SAX/DOM with line numbers available per node

查看:150
本文介绍了有没有办法通过SAX / DOM解析XML,每个节点可以使用行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个大型XML文档格式的DOM解析器,它包含许多可以用于自动生成Java代码的项目。这仅限于小型表达式,然后将其合并到动态生成的Java源文件中。

I already have written a DOM parser for a large XML document format that contains a number of items that can be used to automatically generate Java code. This is limited to small expressions that are then merged into a dynamically generated Java source file.

到目前为止 - 很好。一切正常。

So far - so good. Everything works.

但是,我希望能够嵌入包含Java代码的XML节点的行号(以便如果配置包含无法编译的代码,每个方法都将有一个指向源XML文档的指针和行号以方便调试)。我在parse-time不需要行号,我不需要验证XML源文档,并在特定行号上发出错误。我需要能够访问我的DOM或每个SAX事件中每个节点和属性的行号。

BUT - I wish to be able to embed the line number of the XML node where the Java code was included from (so that if the configuration contains uncompilable code, each method will have a pointer to the source XML document and the line number for ease of debugging). I don't require the line number at parse-time and I don't need to validate the XML Source Document and throw an error at a particular line number. I need to be able to access the line number for each node and attribute in my DOM or per SAX event.

有关如何实现此目的的任何建议?

Any suggestions on how I might be able to achieve this?

PS
另外,我读了StAX有一个解析行获得行号的方法,但理想情况下,我希望通过Java 4/5中的常规SAX / DOM处理实现相同的结果,而不是成为Java 6+应用程序,承担额外的.jar文件。

P.S. Also, I read the StAX has a method to obtain line number whilst parsing, but ideally I would like to achieve the same result with regular SAX/DOM processing in Java 4/5 rather than become a Java 6+ application or take on extra .jar files.

推荐答案

也许与 org.xml.sax.Locator

这篇关于有没有办法通过SAX / DOM解析XML,每个节点可以使用行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆