Home  >  Article  >  Java  >  How to Handle XML Documents with Default Namespace When Using XPath?

How to Handle XML Documents with Default Namespace When Using XPath?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-24 06:23:01833browse

How to Handle XML Documents with Default Namespace When Using XPath?

Using XPath on XML Documents with Default Namespace

When dealing with XML documents that employ a default namespace, the absence of a prefix can make XPath manipulation challenging. While setting the namespaceAware property to false may seem like a solution, it has its limitations.

To manipulate such documents effectively, one must consider the following:

Using a Namespace Context

When working with namespace-qualified documents, a NamespaceContext can be employed during XPath evaluation. This context allows you to specify prefixes for namespace URIs. While the prefixes used in the context need not match those in the document, the fragments in the XPath must be prefixed accordingly.

For instance, consider the following code:

<code class="java">import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

public class Demo {

    public static void main(String[] args) {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true);
        try {
            DocumentBuilder builder = domFactory.newDocumentBuilder();
            Document dDoc = builder.parse("E:/test.xml");

            XPath xPath = XPathFactory.newInstance().newXPath();
            xPath.setNamespaceContext(new MyNamespaceContext());
            NodeList nl = (NodeList) xPath.evaluate("/ns:root/ns:author", dDoc, XPathConstants.NODESET);
            System.out.println(nl.getLength());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static class MyNamespaceContext implements NamespaceContext {

        public String getNamespaceURI(String prefix) {
            if("ns".equals(prefix)) {
                return "http://www.mydomain.com/schema";
            }
            return null;
        }

        public String getPrefix(String namespaceURI) {
            return null;
        }

        public Iterator getPrefixes(String namespaceURI) {
            return null;
        }

    }

}</code>

Note: The XPath expression "/ns:root/ns:author" uses prefixes consistent with the NamespaceContext.

Alternative Approach

In some cases, the following approach could also be effective:

<code class="java">import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

public class Demo {

    public static void main(String[] args) {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = domFactory.newDocumentBuilder();
            Document dDoc = builder.parse("E:/test.xml");

            XPath xPath = XPathFactory.newInstance().newXPath();
            NodeList nl = (NodeList) xPath.evaluate("/root/author", dDoc, XPathConstants.NODESET);
            System.out.println(nl.getLength());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}</code>

Note: This approach assumes that the default namespace is not used elsewhere in the document.

The above is the detailed content of How to Handle XML Documents with Default Namespace When Using XPath?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn