如何获得JDOM/XPath忽略名称空间? [英] How can I get JDOM/XPath to ignore namespaces?

查看:96
本文介绍了如何获得JDOM/XPath忽略名称空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要处理XML DOM,最好使用JDOM,在这里我可以在节点上进行XPath搜索.我知道节点名称或路径,但是我想完全忽略命名空间,因为有时文档带有命名空间,有时没有命名空间,并且我不能依赖特定的值.那可能吗?怎么样?

I need to process an XML DOM, preferably with JDOM, where I can do XPath search on nodes. I know the node names or paths, but I want to ignore namespaces completely because sometimes the document comes with namespaces, sometimes without, and I can't rely on specific values. Is that possible? How?

推荐答案

我知道这个问题有点老了,但是对于以后查看此问题的人来说,您可以重写一些JDOM默认类以有效地使它也忽略命名空间.您可以将自己的JDOMFactory实现传递给SAXBuilder,该实现将忽略传递给它的所有Namespace值.

I know this question is a little old, but for those viewing this later, you can override a few JDOM default classes to effectively make it ignore namespaces as well. You can pass your own JDOMFactory implementation to the SAXBuilder that ignores all Namespace values passed into it.

然后重写SAXBuilder类并实现createContentHandler方法,以便它为startPrefixMapping方法返回带有空白定义的SAXHandler.

Then override the SAXBuilder class and implement the createContentHandler method so that it returns a SAXHandler with a blank definition for the startPrefixMapping method.

我没有在生产环境中使用此功能,所以请注意,但我已经验证了它确实可以在我完成的一些快速而肮脏的XML工作中使用.

I haven't used this in a production setting so caveat emptor, but I have verified that it does work on some quick and dirty XML stuff I've done.

这篇关于如何获得JDOM/XPath忽略名称空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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