如何使用Xpath java解析xml与命名空间

时间:2017-01-11 10:54:57

标签: java xml xpath

我有一个具有命名空间但没有前缀的xml。我有xpaths但前缀是从Schematron生成的。我想使用带前缀的xpath从该xml中获取元素细节。

这是我的XML

<?xml version="1.0" encoding="UTF-8"?>
<section xmlns="http://foo" type="abc" id="ad">
    <title>Document Title</title>
    <body>
        <p>
            <t>Document Body</t>
        </p>
        <p>
            <t>Document Body</t>
        </p>
        <p>
            <t>Document Body</t>
        </p>
    </body>
</section>

这是java代码

package com;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XPathTest {
    public static void main(String[] args) {
        File inputFile = new File("files/my_xml.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder;
        try {
            dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(inputFile);
            doc.getDocumentElement().normalize();

            XPath xPath = XPathFactory.newInstance().newXPath();

            HashMap<String, String> prefMap = new HashMap<String, String>() {
                {
                    put("q", "http://foo");
                }
            };
            SmartContentNameSpaceContext namespaces = new SmartContentNameSpaceContext(prefMap);
            xPath.setNamespaceContext(namespaces);

            String expression = "//q:section[1]";
            NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET);

            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);

                System.out.println(node.getAttributes().item(0));
                System.out.println("current element : " + node.getNodeName());
            }
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (XPathExpressionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

我读到了关于NameSpaceContext的界面,所以我试过了

HashMap<String, String> prefMap = new HashMap<String, String>() {
                {
                    put("q", "foo");
                }
            };
MyNameSpaceContext namespaces = new MyNameSpaceContext(prefMap);
xPath.setNamespaceContext(namespaces);

这是MyNameSpaceContext

package com;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.xml.namespace.NamespaceContext;

public class MyNameSpaceContext implements NamespaceContext {

    private final Map<String, String> PREF_MAP = new HashMap<String, String>();

    public SmartContentNameSpaceContext(final Map<String, String> prefMap) {
        PREF_MAP.putAll(prefMap);
    }

    @Override
    public String getNamespaceURI(String prefix) {
        return PREF_MAP.get(prefix);
    }

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

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

}

但这不起作用?

1 个答案:

答案 0 :(得分:1)

您需要确保使用

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
dbFactory.setNamespaceAware(true);

如果要在DOM树上使用XPath。我没有进一步研究其他问题。