如何在 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.
}
}
}
你把两个方法 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 类,是一个非常糟糕的设计 - 您不仅必须提供从未使用过的两个方法getPrefix
和getPrefixes
,而且该类无法发现所有已绑定的(前缀,命名空间)对。如果你想要更好的东西,转向Saxon的s9api界面,然后你可以使用XPath 3.1作为奖励。