我正在尝试获得一个名为" value"从这个XML文档中读取文本内容:
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>com.gmail.pavilion99</groupId>
<artifactId>ValdeonScrim</artifactId>
<version>0.8.16-20140802.182613-1</version>
<versioning>
<snapshot>
<timestamp>20140802.183132</timestamp>
<buildNumber>2</buildNumber>
</snapshot>
<lastUpdated>20140802183148</lastUpdated>
<snapshotVersions>
<snapshotVersion>
<extension>pom</extension>
<value>0.8.16-20140802.183132-2</value>
<updated>20140802183132</updated>
</snapshotVersion>
<snapshotVersion>
<extension>jar</extension>
<value>0.8.16-20140802.183132-2</value>
<updated>20140802183132</updated>
</snapshotVersion>
</snapshotVersions>
</versioning>
</metadata>
使用这个java代码:
Document doc = builder.parse(new URL("http://localhost:8081/artifactory/list/plugins-snapshot-local/com/gmail/pavilion99/ValdeonScrim/maven-metadata.xml").openStream());
NodeList nodes = doc.getElementsByTagName("latest");
Document doc2 = builder.parse(new URL("http://localhost:8081/artifactory/list/plugins-snapshot-local/com/gmail/pavilion99/ValdeonScrim/"+nodes.item(0).getTextContent()+"/maven-metadata.xml").openStream());
NodeList nodes2 = doc2.getElementsByTagName("value");
for(int i = 0; i < nodes2.getLength(); i++){
getServer().getLogger().info(nodes2.item(i).getNodeName());
}
String downloadPath = nodes.item(0).getTextContent();
downloadPath += "/ValdeonScrim-";
downloadPath += nodes2.item(0).getTextContent();
但是我在最后一行收到NullPointerException
错误,这意味着它没有找到任何节点。我尝试使用XPath以及尝试此方法的其他方法几个小时都在撕扯我的头发,我无法弄清楚为什么它没有在元数据/版本控制/ snapshotVersions中找到value
元素/ snapshotVersion /值。
谢谢!