开发者

JAXP XPathFactory Namespaces

I am trying to retrieve one node from a xml doc in android(java).

<?xml version="1.0" encoding="utf-8" ?>
<config:Manifest xmlns:config="http://leaflabs.com/manifest.config.xsd"> 
  <config:Text config:name="siteowner" config:desc="Site owner" config:transform="title"/>
  <config:Text config:name="siteowner1" config:desc="Site owner" config:transform="title"/>
</config:Manifest>

XPATH

config:Manifest/config:Text[@config:name='siteowner']

I am using JAXP XPathFactory. The problem i am getting is get null back everytime.

I made sure my xpath was correct made sure my document builder NamespaceAware is set to true and i even followed a Example (at the bottom of page) that implement the Namesp开发者_JAVA技巧aceContext but i still get nothing.

I looked at a stackoverflow Post but nobody answered the guy Link

What am i doing wrong


The issue in your code is the factory is not namespace aware. There is another thread that resolves this problem, How to use XPath on xml docs having default namespace

Suggest you try something like this,

XPath xPath = XPathFactory.newInstance().newXPath(); xPath.setNamespaceContext(new MyNamespaceContext());


Not a 100% sure but I believe you need a token in front of you xpath statement:

I would try the following:

$this/config:Manifest/config:Text[@config:name='siteowner']

And if that doesn't work try this:

/config:Manifest/config:Text[@config:name='siteowner']

And if neither of those work try this:

//config:Manifest/config:Text[@config:name='siteowner']

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜