-1

我有以下xml:

<version>
    <name>2.0.2</name>
    <description>
-Stop hsql database after close fist <br />
-Check for null category name before adding it to the categories list  <br />
-Fix NPE bug if there is no updates  <br />
-add default value for variable, change read bytes filter, and description of propertyFile  <br />
-Change HTTP web Proxy (the “qcProxy” field ) to http://web-proxy.isr.hp.com:8080  <br />
</description>
    <fromversion>>=2.0</fromversion>
</version>

我想使用 Java 返回描述标签字符串内容?

4

1 回答 1

2

This is pretty standard Java XML parsing, you can find it anywhere on the internet, but it goes like this using XPath in standard JDK.

String xml = "your XML";

// load the XML as String into a DOM Document object
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
ByteArrayInputStream bis = new ByteArrayInputStream(xml.getBytes());
Document doc = docBuilder.parse(bis);

// XPath to retrieve the content of the <version>/<description> tag
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile("/version/description");
Node description = (Node)expr.evaluate(doc, XPathConstants.NODE);
System.out.println("description: " + description.getTextContent());

Edit

Since you are having XML <br/> in your text content, it cannot be retrieved from Node.getTextContent(). One solution is to transform that Node to XML String equivalent, stripping the root node <description>.

This is a complete example:

String xml = "<version>\r\n" + //
        "    <name>2.0.2</name>\r\n" + //
        "    <description>\r\n" + //
        "-Stop hsql database after close fist <br />\r\n" + //
        "-Check for null category name before adding it to the categories list  <br />\r\n" + //
        "-Fix NPE bug if there is no updates  <br />\r\n" + //
        "-add default value for variable, change read bytes filter, and description of propertyFile  <br />\r\n" + //
        "-Change HTTP web Proxy (the “qcProxy” field ) to http://web-proxy.isr.hp.com:8080  <br />\r\n" + //
        "</description>\r\n" + //
        "    <fromversion>>=2.0</fromversion>\r\n" + //
        "</version>";

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
ByteArrayInputStream bis = new ByteArrayInputStream(xml.getBytes());
Document doc = docBuilder.parse(bis);

// XPath to retrieve the <version>/<description> tag
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile("/version/description");
Node descriptionNode = (Node) expr.evaluate(doc, XPathConstants.NODE);

// Transformer to convert the XML Node to String equivalent
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter sw = new StringWriter();
transformer.transform(new DOMSource(descriptionNode), new StreamResult(sw));
String description = sw.getBuffer().toString().replaceAll("</?description>", "");
System.out.println(description);

prints:

-Stop hsql database after close fist <br/>
-Check for null category name before adding it to the categories list  <br/>
-Fix NPE bug if there is no updates  <br/>
-add default value for variable, change read bytes filter, and description of propertyFile  <br/>
-Change HTTP web Proxy (the “qcProxy” field ) to http://web-proxy.isr.hp.com:8080  <br/>

Edit 2

In order to have them all you need to get a NODESET of the different nodes and iterate over it to do the exact same operation as above.

// XPath to retrieve the content of the <version>/<description> tag
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile("//description");
NodeList descriptionNode = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

List<String> descriptions = new ArrayList<String>(); // hold all the descriptions as String
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
for (int i = 0; i < descriptionNode.getLength(); ++i) {
    Node descr = descriptionNode.item(i);
    StringWriter sw = new StringWriter();
    transformer.transform(new DOMSource(descr), new StreamResult(sw));
    String description = sw.getBuffer().toString().replaceAll("</?description>", "");
    descriptions.add(description);
}
// here you can do what you want with the List of Strings `description`
于 2012-11-19T10:14:58.080 回答