繁体   English   中英

用于pom的Java XPath表达式

[英]Java XPath expression for pom

java - xpath - pom.xml

我使用以下pom.xml文件作为xml dom资源,我想得到标签<version>值而不在我的代码中迭代这就是为什么我使用xpath但我猜我的xpath express不正确这就是为什么它没有返回任何值。 任何帮助赞赏

java代码

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

    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse("C:\\projects\\pom.xml");
    XPath xpath = XPathFactory.newInstance().newXPath();

    XPathExpression expr = xpath.compile("project/version");
    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    for (int i = 0; i < nodes.getLength(); i++) {
        System.out.println(nodes.item(i).getNodeValue());
    }

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.dc.app</groupId>
  <artifactId>rum</artifactId>
  <packaging>war</packaging>
  <version>12.54-SNAPSHOT</version>
  <name>rum</name>
  <description>rum</description>
</project>

创建一个javax.xml.NamespaceContext并将其提供给您的xpath:

xpath.setNamespaceContext(ctx);

所以:

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

DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("blah.pom");
XPath xpath = XPathFactory.newInstance().newXPath();
Map<String, String> namespaces = new HashMap<String, String>();
namespaces.put("pom", "http://maven.apache.org/POM/4.0.0");

xpath.setNamespaceContext(
        new NamespaceContextImpl("http://maven.apache.org/POM/4.0.0", 
                namespaces));

XPathExpression expr = xpath.compile("/pom:project/pom:version");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
    System.out.println(nodes.item(i).getTextContent());
}

收益率:

12.54-SNAPSHOT

编辑:NamespaceContext的实现:

public class NamespaceContextImpl implements NamespaceContext {
   private final Map<String, String> namespaces;
   private final String defaultNamespaceURI;

   public NamespaceContextImpl(String defaultNamespaceURI,
                               Map<String, String> namespaces) {
      this.defaultNamespaceURI = defaultNamespaceURI;
      this.namespaces = namespaces;
   }

   public Iterator getPrefixes(String namespaceURI) {
      throw new IllegalStateException("Not Implemented.");
   }

   public String getPrefix(String namespaceURI) {
      throw new IllegalStateException("Not Implemented.");
   }

   public String getNamespaceURI(String prefix) {
      if (prefix == null) {
         throw new IllegalArgumentException();
      }
      if (prefix.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
         return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
      }
      if (prefix.equals(XMLConstants.XML_NS_PREFIX)) {
         return XMLConstants.XML_NS_URI;
      }
      if (prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)) {
         return defaultNamespaceURI;
      }
      String result = namespaces.get(prefix);
      if (result == null) {
         result = XMLConstants.NULL_NS_URI;
      }
      return result;
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM