0

我正在尝试读取远程 XML 文件的“发布”标签的值并返回它的值。我可以使用 getElementText() 但不能通过 getElementValue() 找到“发布”标签的值

Java代码..

 try {
URL url1 = new URL("http://hsv-artifactory.emrsn.org:8081/artifactory/libs-release-local/com/avocent/commonplatform/cps/symbols/gdd/GDDResources/maven-metadata.xml");
XMLStreamReader reader1 = XMLInputFactory.newInstance().createXMLStreamReader(url1.openStream());
String Latest = null;
while (reader1.hasNext()) {
    if (reader1.next() == XMLStreamConstants.START_ELEMENT) {
        if (reader1.getLocalName().equals("release")) {
            Latest = reader1.getElementText();
            break;
        }
    }
}
System.out.println("Latest version in Artifactory is :"+Latest);
} catch (IOException ex) {
 // handle exception
Logger.getLogger(SVNRepoConnector1.class.getName()).log(Level.SEVERE, null, ex);
} catch (XMLStreamException ex) {
// handle exception
Logger.getLogger(SVNRepoConnector1.class.getName()).log(Level.SEVERE, null, ex);
} finally {
// close the stream

        }  

在上面的代码中,值被存储在一个字符串变量中,但我想把它存储在一个整数变量中,这样我就可以在之后执行加法、减法等操作。请帮助

4

1 回答 1

0

DOM 解决方案:

URL url1 = new URL("http://hsv-artifactory.emrsn.org:8081/artifactory/libs-release-local/com/avocent/commonplatform/cps/symbols/gdd/GDDResources/maven-metadata.xml");
InputSource xmlInputSource = new InputSource(url1.openStream());
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document dom = dBuilder.parse(xmlInputSource);
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("//release");//all release elements
NodeList nodes = (NodeList) expr.evaluate(e,XPathConstants.NODESET);
ArrayList<Integer> releaseList = new ArrayList<Integer>();
for (int i = 0; i < nodes.getLength(); i++) {
   Element releaseElem = (Element) nodes.item(i);
    releaseList.add(Integer.parseInt(releaseElem.getText()));
}

只需捕获异常。

于 2014-11-25T14:08:53.343 回答