问题
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