Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从xml文档获取html字符串_Java_Xml_Xml Parsing - Fatal编程技术网

Java 从xml文档获取html字符串

Java 从xml文档获取html字符串,java,xml,xml-parsing,Java,Xml,Xml Parsing,我有以下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 upda

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

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

-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

为了获得所有这些节点,您需要获得不同节点的节点集,并对其进行迭代,以执行与上面完全相同的操作

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

请为这一要求提供适当的背景,因为它在当前状态下过于宽泛和模糊。谢谢到目前为止你试过什么?作为1000多名用户,您必须意识到,我们不会为您编码:-使用本文档的xsd,并使用任何XML解析实现,如apache xmlbeans、jaxb等。这将是最干净的方法。我使用了此代码,但问题是我在该标记中包含html代码。如果我尝试了你的代码,我只会得到第一行-关闭后停止hsql数据库。我有多个节点的描述标签。我需要一个解决方案将它们作为数组或任何其他数据结构返回。@FerasOdeh请参阅我的第二次编辑。如果下次你能直接解释你的整个问题,那就太酷了-
// 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`