Finding the exact location of an Element in a XML-Document

寵の児 提交于 2019-12-10 11:48:01

问题


I'm looking for a way to find the exact location of an Element within a XML-Document. I parse my Document with XOM and process it in the usual way. The tricky thing is, that in the XML document, some scripts are embedded (just text nodes) and in the case the scripts fail (parsing, logic, whatever) I want to report the user (which writes those scripts) the exact line number the script has been declared.

The most ideal way would be something like this:

int scriptLine = someElement.getFirstChildElement("script").getDeclaringLineNumber();

Sadly I couldn't find a way to do this, with or without XOM. If anyone has ideas or has already done something like this - I could use some help. :-)


回答1:


Way back I did this with JDOM. By extending the SAXBuilder and intercepting the startElement() and endElement() callback, specialized Element implementations could be updated with line number information usig the SAX Locator. The code should still be around .... yes here:

http://jdom.org/dist/binary/jdom-contrib-1.1.1.zip

Search for LineNumberSAXBuilder in the src directory.

I belive XOM has a NodeFactory that could be extended in the same manner.



来源:https://stackoverflow.com/questions/7501326/finding-the-exact-location-of-an-element-in-a-xml-document

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!