문제

I have the following xml:

<config xmlns="http://www.someurl.com">
  <product>
    <brand>
      <content />
    </brand>
  </product>
</config>

I'm reading it nicely into JDOM.

However, when I try to use Jaxen to grab the contents, I can't seem to get anything.

Here's an example of what doesn't seem to work:

XPath xpath = new JDOMXPath("config");

SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext();
namespaceContext.addNamespace("", "http://www.someurl.com");

xpath.setNamespaceContext(namespaceContext);

assert xpath.selectNodes(document).size() > 0 : "should find more than 0";

This assertion always fails.

What am I doing wrong?

도움이 되었습니까?

해결책

You have to assign a prefix. Make that call addNamespace("hopfrog", "http://..."); Then make the XPath ("hopfrog:config");

Keep in mind that the prefixes in XML aren't part of the real data model. The real data model assigns a URL, possibly blank, to each element and attribute. You can use any prefix you want in XPath so long as it's bound to the right URL. Since the URL you want it blank, you bind a prefix to 'blank'.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top