在XML文档中查找元素的确切位置 [英] Finding the exact location of an Element in a XML-Document

查看:97
本文介绍了在XML文档中查找元素的确切位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来查找XML文档中元素的确切位置.我使用XOM解析我的文档,并以通常的方式对其进行处理.棘手的是,在XML文档中,嵌入了一些脚本(仅是文本节点),并且在脚本失败(解析,逻辑等)的情况下,我想向用户(写那些脚本)报告确切的行号该脚本已被声明.

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();

可悲的是,无论有没有XOM,我都找不到解决办法.如果有人有想法或已经做过这样的事情-我可以寻求帮助. :-)

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. :-)

推荐答案

回溯,我使用JDOM做到了这一点.通过扩展SAXBuilder并拦截startElement()和endElement()回调,可以使用与SAX Locator相同的行号信息来更新专用的Element实现.代码仍然应该在...附近.

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

在src目录中搜索LineNumberSAXBuilder.

Search for LineNumberSAXBuilder in the src directory.

我相信XOM有一个NodeFactory,可以用相同的方式扩展.

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

这篇关于在XML文档中查找元素的确切位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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