Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get JDOM/XPath to ignore namespaces?

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?

like image 896
AdSR Avatar asked Apr 09 '10 12:04

AdSR


3 Answers

/ns:foo/ns:bar/@baz

becomes

/*[local-name() = 'foo']/*[local-name() = 'bar']/@baz

You get the point. Don't expect that to be lightning-fast either.

like image 86
Tomalak Avatar answered Nov 09 '22 09:11

Tomalak


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.

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

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.

like image 20
jhulford Avatar answered Nov 09 '22 09:11

jhulford


Here is a jDOM2 solution that has been running in a production setting for a year with no trouble.

public class JdomHelper {

    private static final SAXHandlerFactory FACTORY = new SAXHandlerFactory() {
        @Override
        public SAXHandler createSAXHandler(JDOMFactory factory) {
            return new SAXHandler() {
                @Override
                public void startElement(
                        String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
                    super.startElement("", localName, qName, atts);
                }
                @Override
                public void startPrefixMapping(String prefix, String uri) throws SAXException {
                    return;
                }
            };
        }
    };


    /** Get a {@code SAXBuilder} that ignores namespaces.
     * Any namespaces present in the xml input to this builder will be omitted from the resulting {@code Document}. */
    public static SAXBuilder getSAXBuilder() {
        // Note: SAXBuilder is NOT thread-safe, so we instantiate a new one for every call.
        SAXBuilder saxBuilder = new SAXBuilder();
        saxBuilder.setSAXHandlerFactory(FACTORY);
        return saxBuilder;
    }

}
like image 38
jaco0646 Avatar answered Nov 09 '22 10:11

jaco0646