tags:

views:

35

answers:

1

I have the following xml:

<config xmlns="http://www.someurl.com"&gt;
  <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?

+1  A: 

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'.

bmargulies
But that doesn't match my xml!Wouldn't that match xml that looks like this:<config xmlns:hopfrog="http://www.someurl.com"/>
JBristow
Sure it does. Prefixes aren't 'real'. The real thing is the url ...
bmargulies