绑定Java中XPath评估的命名空间

问题描述 投票:0回答:1

如何在java中声明一个命名空间,我试图在我的main方法中声明它要求“实现所有抽象方法”,当完成实现java声明用红色下划线50%的代码时。如何申报?以下是我到目前为止所做的事情,这是错误的!请注意,此示例中的xpath是错误的。请忽略它,我只想声明命名空间。

package xpath;

import java.util.Iterator;
import javax.xml.namespace.NamespaceContext;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.xpath.*;

public class Xpath {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)throws Exception {
    DocumentBuilderFactory Factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = Factory.newDocumentBuilder();
    Document doc = builder.parse("C:/Users/HP/Desktop/solution.xml");

    //creating an XPathFactory:
    XPathFactory factory = XPathFactory.newInstance();
    //using this factory to create an XPath object: 
    XPath xpath = factory.newXPath();
xpath.setNamespaceContext( new NamespaceContext() {
    public String getNamespaceURI(String prefix) {
      switch (prefix) {
        case "df": return "http://xml.sap.com/2002/10/metamodel/webdynpro";
        ...
       }
    });
    // XPath Query for showing all nodes value
    XPathExpression expr = xpath.compile("/df:problem/http://df:solutions/df:solution[df:cost/text()=\"505.9208295302417\"]");
    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;

    for (int i = 0; i < nodes.getLength(); i++) {
//System.out.println("test"+nodes.item(i).getTextContent());
        //System.out.println(nodes.item(i).getElementsByTagName("vehicleId").item(0).getTextContent());
        Element el = (Element) nodes.item(i);
            System.out.println("driverid:" + el.getElementsByTagName("driverId").item(i).getTextContent());
            System.out.println("vehicleId:" + el.getElementsByTagName("vehicleId").item(i).getTextContent());
            System.out.println("Citizen:" + el.getElementsByTagName("act").item(i).getTextContent());
        //System.out.println("Element currently in: " + el.getNodeName());

        if (el.getFirstChild().getNodeType() == Node.TEXT_NODE)
            System.out.println("driverid:" + el.getElementsByTagName("vehicleId").item(i).getTextContent());
        Node vehicle = el.getFirstChild().getFirstChild();

        NodeList children = el.getChildNodes();

    }

        @Override
        public String getPrefix(String namespaceURI) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public Iterator getPrefixes(String namespaceURI) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    }

}
java xml xml-namespaces
1个回答
0
投票

您已将两个方法getPrefix()和getPrefixes()放在错误的位置,它们需要位于实现NamespaceContext的匿名类中:

xpath.setNamespaceContext( new NamespaceContext() {
    public String getNamespaceURI(String prefix) {
      switch (prefix) {
        case "df": return "http://xml.sap.com/2002/10/metamodel/webdynpro";
        ...
       }
    }
@Override
        public String getPrefix(String namespaceURI) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public Iterator getPrefixes(String namespaceURI) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        });

Java中的XPath API,特别是NamespaceContext类,是一个非常糟糕的设计 - 你不仅需要提供从未使用过的两个方法getPrefixgetPrefixes,而且这个类没有办法发现所有的(前缀,名称空间)已绑定的对。如果你想要更好的东西,转向Saxon的s9api界面,然后你可以使用XPath 3.1作为奖励。

© www.soinside.com 2019 - 2024. All rights reserved.