久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 CDATA 部分解析帶有 HTML 標記的 XML 文件?

How can i parse an XML file with HTML tags inside CDATA section?(如何在 CDATA 部分解析帶有 HTML 標記的 XML 文件?)
本文介紹了如何在 CDATA 部分解析帶有 HTML 標記的 XML 文件?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<extendedinfo type="html">
    <![CDATA[<table class="ResultTable" cellpadding=2 cellspacing=1 border=0><tr class="TableHeadingLine"><th bgcolor="#b3b3b3" align="left" colspan="6"><font face="arial, verdana, trebuchet, officina, sans-serif" size="+2"><B>Testcase: Init Testreport</B></font></th></tr><tr class="TableHeadingLine"><th class="TableHeadingCell" width="120px"></th><th class="TableHeadingCell" width="120px"></th><th class="TableHeadingCell" width="80px"></th><th class="TableHeadingCell" width="345px"></th><th class="TableHeadingCell" width="345px"></th><th class="TableHeadingCell" width="70px"></th></tr>]]>
</extendedinfo>
<extendedinfo type="html">
    <![CDATA[<tr><td class="DefineCell">58.675124</td><td class="DefaultCell" colspan="5"><i><font color="#008000">Set_Temperature is set to 23</font></i><br>Set_Temperature = 23</td></tr>]]>
</extendedinfo>

我有一個由上述格式的工具生成的 .XML 文件,其中包含 CDATA 部分中的 html 數據.哪個解析器或以什么方式可以使用 java 從 XMLfile 中檢索 html 數據?

I have a .XML file generated by a tool in the above format, with html data within CDATA sections. Which parser or in what way can I retrieve the html data from the XMLfile using java?

推薦答案

只需訪問 CDATA 作為文本內容

Just access the CDATA as text content

    import java.io.BufferedInputStream;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;

public void getCDATAFromHardcodedPathWithDom() {
    String yourSampleFile = "/path/toYour/sample/file.xml";
    String cdataNode = "extendedinfo";
    try (InputStream in =
            new BufferedInputStream(new FileInputStream(yourSampleFile))) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(in);
        NodeList elements = doc.getElementsByTagName(cdataNode);
        for (int i = 0; i < elements.getLength(); i++) {
            Node e = elements.item(i);
            System.out.println(e.getTextContent());
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

變體 2(stax):

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;

public void getCDATAFromHardcodedPathWithStax() {
    String yourSampleFile = "/path/toYour/sample/file.xml";
    String cdataNode = "extendedinfo";
    XMLStreamReader r = null;
    try (InputStream in =
            new BufferedInputStream(new FileInputStream(yourSampleFile));)        {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        r = factory.createXMLStreamReader(in);
        while (r.hasNext()) {
            switch (r.getEventType()) {
            case XMLStreamConstants.START_ELEMENT:
                if (cdataNode.equals(r.getName().getLocalPart())) {
                    System.out.println(r.getElementText());
                }
                break;
            default:
                break;
            }
            r.next();
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (r != null) {
            try {
                r.close();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
}

使用/path/toYour/sample/file.xml

With /path/toYour/sample/file.xml

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<root>
<extendedinfo type="html">
    <![CDATA[<table class="ResultTable" cellpadding=2 cellspacing=1 border=0><tr class="TableHeadingLine"><th bgcolor="#b3b3b3" align="left" colspan="6"><font face="arial, verdana, trebuchet, officina, sans-serif" size="+2"><B>Testcase: Init Testreport</B></font></th></tr><tr class="TableHeadingLine"><th class="TableHeadingCell" width="120px"></th><th class="TableHeadingCell" width="120px"></th><th class="TableHeadingCell" width="80px"></th><th class="TableHeadingCell" width="345px"></th><th class="TableHeadingCell" width="345px"></th><th class="TableHeadingCell" width="70px"></th></tr>]]>
</extendedinfo>
<extendedinfo type="html">
    <![CDATA[<tr><td class="DefineCell">58.675124</td><td class="DefaultCell" colspan="5"><i><font color="#008000">Set_Temperature is set to 23</font></i><br>Set_Temperature = 23</td></tr>]]>
</extendedinfo>
</root>

它會給你

<table class="ResultTable" cellpadding=2 cellspacing=1 border=0><tr class="TableHeadingLine"><th bgcolor="#b3b3b3" align="left" colspan="6"><font face="arial, verdana, trebuchet, officina, sans-serif" size="+2"><B>Testcase: Init Testreport</B></font></th></tr><tr class="TableHeadingLine"><th class="TableHeadingCell" width="120px"></th><th class="TableHeadingCell" width="120px"></th><th class="TableHeadingCell" width="80px"></th><th class="TableHeadingCell" width="345px"></th><th class="TableHeadingCell" width="345px"></th><th class="TableHeadingCell" width="70px"></th></tr>


<tr><td class="DefineCell">58.675124</td><td class="DefaultCell" colspan="5"><i><font color="#008000">Set_Temperature is set to 23</font></i><br>Set_Temperature = 23</td></tr>

這里給出了一個有趣的使用 JAXB 的替代方法:

An interesting alternative using JAXB is given here:

從 CDATA 中檢索值

這里給出了如何提取所有 CDATA 的示例:

An example on how to extract just all CDATA is given here:

無法使用 XMLEventReader 檢查 XML 中的 CDATA稅表

這篇關于如何在 CDATA 部分解析帶有 HTML 標記的 XML 文件?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Upload progress listener not fired (Google drive API)(上傳進度偵聽器未觸發(Google 驅動器 API))
Save file in specific folder with Google Drive SDK(使用 Google Drive SDK 將文件保存在特定文件夾中)
Google Drive Android API - Invalid DriveId and Null ResourceId(Google Drive Android API - 無效的 DriveId 和 Null ResourceId)
Google drive api services account view uploaded files to google drive using java(谷歌驅動api服務賬戶查看上傳文件到谷歌驅動使用java)
Google Drive service account returns 403 usageLimits(Google Drive 服務帳號返回 403 usageLimits)
com.google.api.client.json.jackson.JacksonFactory; missing in Google Drive example(com.google.api.client.json.jackson.JacksonFactory;Google Drive 示例中缺少)
主站蜘蛛池模板: 久草热8精品视频在线观看 午夜伦4480yy私人影院 | re久久| 天天色天天色 | 国产成人精品午夜 | 国产欧美日韩一区二区三区在线观看 | 91久久精品国产91久久性色tv | 日韩免费一区二区 | 亚洲导航深夜福利涩涩屋 | 国产成人精品一区二 | 亚洲电影成人 | 成人在线观看欧美 | 四虎网站在线观看 | 欧美性一区二区三区 | 亚洲国产中文字幕 | 午夜精品视频 | 高清黄色毛片 | 91精品国产91久久久久久吃药 | 黄视频免费在线 | 免费一区二区 | 国产成人综合一区二区三区 | 久久大| 中文字幕91 | 亚洲精品在线视频 | 久久久久久av | 韩日视频在线观看 | 久久久新视频 | 日韩av在线免费 | 日日操操| 中文字幕av高清 | 欧美日韩在线精品 | 欧美成人激情 | 欧美久久电影 | 91欧美精品成人综合在线观看 | 国产成人精品一区二区三区在线观看 | 精品久久一区二区三区 | 国产成人在线一区 | 日日夜夜草 | 日韩av免费看 | 粉嫩av久久一区二区三区 | www.男人天堂.com | 久久久www成人免费无遮挡大片 |