简体   繁体   English

从XML解析CDATA以启用编辑Java

[英]Parse CDATA from XML to enable editing Java

I am writing a simulator which communicates with a client's piece of software over a local socket. 我正在编写一个模拟器,该模拟器通过本地套接字与客户端的软件通信。 The communication language is XML. 通信语言是XML。 I have written some code which works - parsing the incoming XML string into Document via the DocumentBuilder interface. 我编写了一些有效的代码-通过DocumentBuilder接口将传入的XML字符串解析为Document。

I have been encountering a problem with CDATA (Having never seen it before). 我一直在遇到CDATA的问题(以前从未见过)。 Basically, I need to access fields within the CDATA tag and change them. 基本上,我需要访问CDATA标记内的字段并进行更改。 I load up a 'template' XML document (to reply to the messages with) and use values received in the first message inside the response. 我加载了一个“模板” XML文档(用来回复消息),并使用响应中第一条消息中接收到的值。 Some of the fields that need to be changed are in this CDATA tag (clear what I mean below). 一些需要更改的字段都在此CDATA标记中(请清楚我在下面的意思)。

public static String getOutputMessage(String input) throws Exception{
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    Document inputDoc, outputDoc;
    Element messageElement = (Element)inputDoc.getElementsByTagName("TRANS").item(0);
    messageType = messageElement.getAttribute("name");
    if (messageType.equals("processTransaction")){
        outputDoc = db.parse(path+"processTransaction\\posPrintReceipt.xml");
        outputDoc = changeContent(outputDoc, "PAN_NUMBER", transaction.getPan_number());
        outputDoc = changeContent(outputDoc, "TOKEN", transaction.getToken());
        outputDoc = changeContent(outputDoc, "TOTAL_AMOUNT", transaction.getTotal_amount());
        outputDoc = changeContent(outputDoc, "TRANSACTION_TIME", transaction.getTransaction_time());
        outputDoc = changeContent(outputDoc, "TRANSACTION_DATE", transaction.getTransaction_date());
    }
}

private static Document changeContent(Document doc,String tag,String value) {
    System.out.println("Changing: ["+tag+" : "+value+"]");
    NodeList nodes=doc.getElementsByTagName(tag);
    Node node = nodes.item(0);
    Node parent=node.getParentNode();
    node.setTextContent(value);
    System.out.println(doc.getElementsByTagName(tag).item(0) + " " + node.getTextContent());
    parent.replaceChild(node, doc.getElementsByTagName(tag).item(0));
    return doc;
}

The functions above work on normal Elements but below is an example XML message I have to read and change some values such as 上面的功能在普通Elements上起作用,但是下面的示例XML消息我必须阅读并更改一些值,例如

<RLSOLVE_MSG version="5.0">
<MESSAGE>
    <SOURCE_ID>DP01</SOURCE_ID>
    <TRANS_NUM>000001</TRANS_NUM>
</MESSAGE>
<POI_MSG type="interaction">
    <INTERACTION name="posPrintReceipt">
        <RECEIPT type="merchant" format="xml">
            <![CDATA[<RECEIPT>
    <AUTH_CODE>06130</AUTH_CODE>
    <CARD_SCHEME>VISA</CARD_SCHEME>
    <CURRENCY_CODE>GBP</CURRENCY_CODE>
    <CUSTOMER_PRESENCE>internet</CUSTOMER_PRESENCE>
    <FINAL_AMOUNT>1.00</FINAL_AMOUNT>
    <MERCHANT_NUMBER>8888888</MERCHANT_NUMBER>
    <PAN_NUMBER>454420******0382</PAN_NUMBER>
    <PAN_EXPIRY>12/15</PAN_EXPIRY>
    <TERMINAL_ID>04176421</TERMINAL_ID>
    <TOKEN>454420bbbbbkqrm0382</TOKEN>
    <TOTAL_AMOUNT>1.00</TOTAL_AMOUNT>
    <TRANSACTION_DATA_SOURCE>keyed</TRANSACTION_DATA_SOURCE>
    <TRANSACTION_DATE>14/02/2014</TRANSACTION_DATE>
    <TRANSACTION_NUMBER>000001</TRANSACTION_NUMBER>
    <TRANSACTION_RESPONSE>06130</TRANSACTION_RESPONSE>
    <TRANSACTION_TIME>17:13:17</TRANSACTION_TIME>
    <TRANSACTION_TYPE>purchase</TRANSACTION_TYPE>
    <VERIFICATION_METHOD>unknown</VERIFICATION_METHOD>
    <DUPLICATE>false</DUPLICATE>
</RECEIPT>]]>
        </RECEIPT>
    </INTERACTION>
</POI_MSG>

CDATA is an encoding mechanism to include arbitrary data within an XML file. CDATA是一种编码机制,可以在XML文件中包含任意数据。 Everything within CDATA is parsed as a single string when loading the XML into a Document instance. 将XML加载到Document实例时,CDATA中的所有内容都被解析为单个字符串。 If you need to access the contents of the CDATA as a DOM document, you will need to instantiate a second Document object from the string contents, make your changes, then serialize that back to a string and put the string back into a CDATA in the original document. 如果您需要以DOM文档的形式访问CDATA的内容,则需要从字符串内容中实例化第二个Document对象,进行更改,然后将其序列化回字符串,然后将该字符串放回到CDATA中。原始文件。

I dont think CDATA section will be parsed as other regular elements in the XML. 我不认为CDATA部分将被解析为XML中的其他常规元素。 CDATA section is purely to escape any syntax checks. CDATA部分纯粹是为了逃避任何语法检查。 My suggestion would be use a element to represent the data in CDATA section. 我的建议是使用一个元素表示CDATA部分中的数据。 If you still want to use CDATA section, I guess you'll need parse the section as a string and then load the data into a Document. 如果您仍然想使用CDATA节,我想您需要将该节解析为字符串,然后将数据加载到Document中。

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

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