java—如何从xml文件获取html结构

mklgxw1f  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(300)

假设xml文件如下所示:

<!DOCTYPE html [
<!ENTITY ldquo "&#x2665;">
]>
<DATA>
<ROW>
        <Id>29855</Id>
        <content><p>Did the summer fly as fast &ldquo;</p>
                  <a href="https://www.ex.com/" target="_blank"></content>
<ROW>
<ROW>
        <Id>11223</Id>
        <content><p>Fly as fast &ldquo;</p>
                  <a href="https://www.ex.com/" target="_blank"></content>
<ROW>
</DATA>

需求是从xml中获取“id”和“content”。内容应该是html结构,因为它存在于xml文件中。比如:

<p>Fly as fast &ldquo;</p>
                  <a href="https://www.ex.com/" target="_blank">

我试过了,但我得到的内容是一个字符串格式,如:“飞得一样快”
这是我用来解析xml的代码:

File fXmlFile = new File("D:\\customer_connect_posts.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);
            doc.getDocumentElement().normalize();

            System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
            NodeList nList = doc.getElementsByTagName("ROW");
            System.out.println("----------------------------");

            for (int temp = 0; temp < nList.getLength(); temp++) {
                Node nNode = nList.item(temp);
                System.out.println("\nCurrent Element :" + nNode.getNodeName());
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;
                    /*System.out.println("Staff id : "
                                       + eElement.getAttribute("Name"));*/
                    System.out.println("First Name : "
                                       + eElement.getElementsByTagName("Id")
                                         .item(0).getTextContent());
                    System.out.println("Last Name : "
                                       + eElement.getElementsByTagName("content").item(0).getTextContent())
                                         );
}
            }
            } catch (Exception e) {
            e.printStackTrace();
            }

问题是我正在调用“gettextcontent()”方法,该方法返回文本。还有别的办法吗。需要帮助。。。

nnvyjq4y

nnvyjq4y1#

从dom从html获取文本 Node ,您应该将其序列化为html。您可以使用saxon和default来实现这一点 Transformer 类似的问题。

Node content = eElement.getElementsByTagName("content").item(0);
 StringWriter sw = new StringWriter();
 Result result = new StreamResult(sw);
 TransformerFactory factory = new TransformerFactoryImpl();
 Transformer proc = factory.newTransformer();
 proc.setOutputProperty(OutputKeys.METHOD, "html");
 for (int i = 0; i < content.getChildNodes().getLength(); i++) {
     proc.transform(new DOMSource(content.getChildNodes().item(i)), result);
 }
 System.out.println("Content:" + sw.toString().trim());

您应该看到下一个输出:

Current Element :ROW
First Name : 29855
Content:<p>Did the summer fly as fast</p>
        <a href="https://www.ex.com/" target="_blank"></a>

Current Element :ROW
First Name : 11223
Content:<p>Fly as fast</p>
        <a href="https://www.ex.com/" target="_blank"></a>

在你的文件标签里 <ROW> 应该用 </ROW> . 也适用于 <a> . 但是你可以用简化的录音 <a href=... /> .

xghobddn

xghobddn2#

您需要使用cdata或对html进行编码以将html存储在xml中,否则html元素将被解释为xml元素。也是你的 ROW 元素似乎没有关闭。我建议像这样使用cdata:

<DATA>
    <ROW>
        <Id>29855</Id>
        <content><![CDATA[<p>Did the summer fly as fast &ldquo;</p>
            <a href="https://www.ex.com/" target="_blank">]]>
        </content>
    </ROW>
    <ROW>
        <Id>11223</Id>
        <content><![CDATA[<p>Fly as fast &ldquo;</p>
            <a href="https://www.ex.com/" target="_blank">]]>
        </content>
    </ROW>
</DATA>

相关问题