【问题标题】:Getting html string from xml document从 xml 文档中获取 html 字符串
【发布时间】:2012-11-19 09:57:49
【问题描述】:

我有以下 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返回描述标签字符串内容?

【问题讨论】:

  • 请为此要求提供适当的上下文,因为它在当前状态下过于宽泛和模糊。谢谢
  • 到目前为止你尝试过什么?作为 1000 多个用户,您必须知道我们不会为您编写代码 :-)
  • 使用本文档的 xsd 并使用任何 XML 解析实现,如 apache xmlbeans、jaxb 等。这将是最干净的方法。

标签: java xml xml-parsing


【解决方案1】:

这是非常标准的 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());

编辑

由于您的文本内容中有 XML &lt;br/&gt;,因此无法从 Node.getTextContent() 检索它。一种解决方案是将 Node 转换为等效的 XML 字符串,剥离根节点 &lt;description&gt;

这是一个完整的例子:

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);

打印:

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

编辑 2

为了拥有它们,您需要获取不同节点的 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`

【讨论】:

  • 我使用了这段代码,但问题是我在这个标签中有 html 代码。如果我尝试了您的代码,我将只得到第一行(“-紧握拳头后停止 hsql 数据库
    ”)。
  • 我有多个带有描述标签的节点。我需要一个解决方案将它们作为数组或任何其他数据结构返回。
  • @FerasOdeh 查看我的第二次编辑。如果你下次能直接解释你的整个问题会很酷;-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-28
  • 2019-04-14
  • 1970-01-01
  • 2010-10-17
  • 1970-01-01
  • 2016-05-22
  • 1970-01-01
相关资源
最近更新 更多