我正在尝试使用dom4j读取pom.xml。文档正在被解析并成功创建,但我无法读取标记。
的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.cycorax</groupId>
<artifactId>test-version</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.cycorax.AspenVersionMain</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jaxen/jaxen -->
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.6</version>
</dependency>
</dependencies>
</project>
我的java代码:
private static void updateVersion(String pomFile) throws DocumentException {
System.out.println(pomFile);
File inputFile = new File(pomFile);
SAXReader reader = new SAXReader();
Document document = reader.read(inputFile);
System.out.println("Root element : "
+ document.getRootElement().getName());
Document classElement = document.getRootElement().getDocument();
System.out.println(classElement.selectNodes("version"));
System.out.println(classElement.selectSingleNode("version"));
System.out.println(document.selectNodes("project/version"));
System.out.println(document.selectSingleNode("project/version"));
}
代码输出:
H:\Java\test-version\dir\pom.xml
Root element : project
[]
null
[]
请帮助为什么它无法阅读其他标签。