简体   繁体   English

从xml文档获取html字符串

[英]Getting html string from xml document

I have the following xml: 我有以下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>

I want to return description tag string content using Java? 我想使用Java返回描述标签字符串的内容吗?

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. 这是非常标准的Java XML解析,您可以在Internet上的任何位置找到它,但是使用标准JDK中的XPath就像这样。

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() . 由于您的文本内容中包含XML <br/> ,因此无法从Node.getTextContent()检索它。 One solution is to transform that Node to XML String equivalent, stripping the root node <description> . 一种解决方案是将该Node转换为等效的XML String,剥离根节点<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 编辑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. 为了拥有它们,您需要获取不同节点的NODESET并对其进行迭代以执行与上述完全相同的操作。

// 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`

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

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