Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java XPath witn命名空间-查询不提供结果_Java_Xml_Xpath - Fatal编程技术网

Java XPath witn命名空间-查询不提供结果

Java XPath witn命名空间-查询不提供结果,java,xml,xpath,Java,Xml,Xpath,没有使用名称空间,XPath示例运行良好。打印出一份清单。添加名称空间后,不会返回任何结果 如何正确使用名称空间和XPath 我的示例(简化)xml文件是: <?xml version="1.0" encoding="UTF-8"?> <nodespace:Employees xmlns:nodespace="my_unique_namespace_name"> <nodespace:Employee id="1"> <nodes

没有使用名称空间,XPath示例运行良好。打印出一份清单。添加名称空间后,不会返回任何结果

如何正确使用名称空间和XPath

我的示例(简化)xml文件是:

<?xml version="1.0" encoding="UTF-8"?>
<nodespace:Employees xmlns:nodespace="my_unique_namespace_name">
    <nodespace:Employee id="1">
        <nodespace:age>29</nodespace:age>
        <nodespace:name>Pankaj</nodespace:name>
        <nodespace:gender>Male</nodespace:gender>
        <nodespace:role>Java Developer</nodespace:role>
    </nodespace:Employee>
    <nodespace:Employee id="2">
        <nodespace:age>35</nodespace:age>
        <nodespace:name>Lisa</nodespace:name>
        <nodespace:gender>Female</nodespace:gender>
        <nodespace:role>CEO</nodespace:role>
    </nodespace:Employee>
</nodespace:Employees>

29
潘卡吉
男性
Java开发者
35
丽莎
女性
首席执行官
XPath Java源代码是:

public class XpathNamespaceTest {
    public static final String PREFIX_NAME = "bdn";
    public static final String NODESPACE_UNIQUE_NAME = "nodespace";
    public static void main(String[] args) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder;
        Document doc = null;
        try {
            ClassLoader classLoader = new XpathNamespaceTest().getClass().getClassLoader();
            URL resource = classLoader.getResource("employees_namespace.xml");
            File file = new File( resource.getFile());
            builder = factory.newDocumentBuilder();
            doc = builder.parse(file);
            XPathFactory xpathFactory = XPathFactory.newInstance();
            XPath xpath = xpathFactory.newXPath();
            xpath.setNamespaceContext(new NamespaceContext() {
                @Override
                public Iterator getPrefixes(String arg0) { return null; }
                @Override
                public String getPrefix(String ns) {
                    if(ns.equals(NODESPACE_UNIQUE_NAME)) {
                        return PREFIX_NAME;
                    }
                    return null;
                }
                @Override
                public String getNamespaceURI(String arg0) {
                    if (PREFIX_NAME.equals(arg0)) {
                        return NODESPACE_UNIQUE_NAME;
                    }
                    return null;
                }
            });

            List<String> names = getEmployeeNameWithAge(doc, xpath, 30);
            System.out.println("Employees with 'age>30' are:" + Arrays.toString(names.toArray()));
        } catch (ParserConfigurationException | SAXException | IOException e) {
            e.printStackTrace();
        }
    }
    private static List<String> getEmployeeNameWithAge(Document doc, XPath xpath, int age) {
        List<String> list = new ArrayList<>();
        try {
            XPathExpression expr = xpath.compile("/bdn:Employees/bdn:Employee[bdn:age>" + age + "]/bdn:name/text()");
            NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
            for (int i = 0; i < nodes.getLength(); i++) list.add(nodes.item(i).getNodeValue());
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
        return list;
    }
}
公共类XpathNamespaceTest{
公共静态最终字符串前缀_NAME=“bdn”;
公共静态最终字符串NODESPACE\u UNIQUE\u NAME=“NODESPACE”;
公共静态void main(字符串[]args){
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
文档生成器;
单据单据=空;
试一试{
ClassLoader ClassLoader=新的XpathNamespaceTest().getClass().getClassLoader();
URL resource=classLoader.getResource(“employees_namespace.xml”);
File File=新文件(resource.getFile());
builder=factory.newDocumentBuilder();
doc=builder.parse(文件);
XPathFactory XPathFactory=XPathFactory.newInstance();
XPath=xpathFactory.newXPath();
setNamespaceContext(新的NamespaceContext(){
@凌驾
公共迭代器getPrefixes(字符串arg0){return null;}
@凌驾
公共字符串getPrefix(字符串ns){
if(ns.equals(NODESPACE_UNIQUE_NAME)){
返回前缀名称;
}
返回null;
}
@凌驾
公共字符串getNamespaceURI(字符串arg0){
if(前缀_NAME.equals(arg0)){
返回节点空间的唯一名称;
}
返回null;
}
});
列表名称=getEmployeeNameWithAge(doc,xpath,30);
System.out.println(“年龄大于30岁的员工是:”+array.toString(names.toArray()));
}捕获(ParserConfiguration异常| SAXException | IOE异常){
e、 printStackTrace();
}
}
私有静态列表getEmployeeNameWithAge(文档文档文档,XPath,int-age){
列表=新的ArrayList();
试一试{
XPathExpression expr=xpath.compile(“/bdn:Employees/bdn:Employees[bdn:age>”+age+”]/bdn:name/text()”;
NodeList节点=(NodeList)expr.evaluate(doc,XPathConstants.NODESET);
对于(inti=0;i
名称空间URI是
my\u unique\u namespace\u name
,因此您需要

public static final String NODESPACE_UNIQUE_NAME = "my_unique_namespace_name";

伟大的非常感谢你!