如何解析CDATA? [英] How can I parse CDATA?

查看:151
本文介绍了如何解析CDATA?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何找到并遍历CDATA下存在的所有节点,这些节点由(<)启动并由(>)关闭?

How can I find and iterate through all the nodes present under CDATA and those nodes are started by (<) and closed by (>)?

此外,我应该如何遍历所有子节点并获得类似于下面的子节点的值?我想检索该值.

Also, how should I iterate over all the child nodes and get the values like in below child node? I want to retrieve the value.

输入XML

    <SOURCE TransactionId="1" ProviderName="ABCDD"><RESPONSE><![CDATA[<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><NetworkResponse xmlns="http://www.example.com/"><NetworkResult>&lt;Network offering_id="13" transaction_id="2" submission_id="3" timestamp="20140828  16010683 GMT" customer_id="NETTest"&gt;
        &lt;Network_List&gt;
            &lt;Network_Info att0="Y" att1="N" att2="N" att3="Y" att4="Y"&gt;
            &lt;SIM_DATA&gt;
                    &lt;SIM&gt;&lt;![CDATA[1100040101]]&gt;&lt;/SIM&gt;
    &lt;/SIM_DATA&gt;
    &lt;NetworkResponseInfo k_status="C"&gt;
                    &lt;KEY1&gt;269&lt;/KEY1&gt;
                    &lt;PARENTNODE&gt;
                        &lt;CHILDNODE1&gt;
                            &lt;KEY2&gt;XXXXXXX&lt;/KEY2&gt;
                            &lt;KEY3&gt;YYYYYYY&lt;/KEY3&gt;
                        &lt;/CHILDNODE1&gt;
                        &lt;CHILDNODE2&gt;
                            &lt;KEY4&gt;N&lt;/KEY4&gt;
                            &lt;KEY5&gt;I&lt;/KEY5&gt;
                        &lt;/CHILDNODE2&gt;
                        &lt;CHILDNODE3&gt;
                            &lt;KEY6&gt;1&lt;/KEY6&gt;
                            &lt;KEY7&gt;3&lt;/KEY7&gt;
                        &lt;/CHILDNODE3&gt;
                    &lt;/PARENTNODE&gt;
                        &lt;KEY8&gt;&lt;![CDATA[some image not visible]]&gt;&lt;/KEY8&gt;
                        &lt;KEY9&gt;N&lt;/KEY9&gt;
                        &lt;KEY10&gt;15&lt;/KEY10&gt;
                &lt;/NetworkResponseInfo&gt;
            &lt;/Network_Info&gt;
        &lt;/Network_List&gt;
        &lt;response_message_list transaction_status_code="000" transaction_status_text="Successful"/&gt;
    &lt;/Network&gt;</NetworkResult></NetworkResponse></soap:Body></soap:Envelope>]]></RESPONSE></SOURCE>

输出XML

                <ns3:NetworkResponse>
                            <Networks_OF_List>
                                <NetCharSeq>
                                    <Nrep>
                                        <type>Some Image</type>
                                        <data>  Data Coming from KEY8 CDATA section</data>
                                    </Nrep>
                                    <Nrep>
                                        <type>ANYTHING</type>
                                        <data>VALUE INSIDE SIM CDATA</data>
                                    </Nrep>
                                    <NetDetail>
                                        <MYKEY1>Value present inside KEY4</MYKEY1>
                                        <MYKEY2>Value present inside KEY5</MYKEY2>
                                    </NetDetail>
                                    <SystemID>Value of KEY2</SystemID>
                                    <SystemPath>Valuelue of KEY3</SystemPath>
                                </NetCharSeq>
                            </Networks_OF_List>
                        </ns3:NetworkResponse>

推荐答案

(欢迎您参加.请注意,某些用户对您不满意,因为您没有显示到目前为止所做的事情.请看如何提问部分,以学习如何提出实际上可以回答的问题,并被认为是SO格式的正确问题.)

(Welcome at SO. Please note that you are downvoted by some users because you do not show what you have done so far. Have a look at the How To Ask section to learn how to ask questions that actually can be answered and are considered proper questions in the SO format.)

如果可以使用XSLT 3.0,则可以考虑使用新的

If you can use XSLT 3.0, you can consider using the new fn:parse-xml function, which will take a document-as-a-string.

但是,您的CDATA节包含自身的转义数据,这意味着应用fn:parse-xml后,您将不得不再次对作为NetworkResult子级的文本节点执行此操作.

However, your CDATA-section contains itself escaped data, which means that, after you apply fn:parse-xml, you will have to do it once again for the text node that is the child of NetworkResult.

更好的解决方案通常是从源头修复此问题,并创建允许某些元素中包含其他XML的XML格式(您可以使用适当的XSD允许它).它将为您省去很多麻烦,并且至少可以对XML进行预验证.

A better solution is often to fix this at the source and creating an XML format that allows other XML in certain elements (you can allow this with a proper XSD). It will save you a lot of trouble and at least you XML can then be pre-validated.

如果您坚持使用XSLT 2.0或1.0,则可以使用disable-output-escaping(在Google上,有很多有关如何使用它的信息),但是您将不得不再次处理输出,因为所使用的两次转义符.您可能需要考虑使用 XProc管道来简化该过程.

If you are stuck with XSLT 2.0 or 1.0, you can use disable-output-escaping (google it, there is a lot of info around on how to use it), but you will have to re-process your output once more because of the double-escape that is used. You may want to consider an XProc pipeline to ease the process.

您写道:此外,我应该如何遍历所有子节点并获得类似于下面的子节点的值

这就是XSLT的全部内容,请阅读此 XSLT教程或任何其他教程您会发现的,它会在最初的几分钟内向您解释.

That is what XSLT is all about, please read this XSLT Tutorial, or any other tutorial you can find, it will be explained to you in the first minutes.

更新:如michael.hor257k在评论中所建议,您还可以使用字符串操作功能手动解析转义的数据.正如他在评论中已经说过的那样,这很麻烦且容易出错,但有时尤其如此.如果在转义后XML不是真正的XML,而是类似 的XML,那么这可能是您唯一的选择.

Update: as suggested by michael.hor257k in the comments, you can also parse the escaped data by hand using string manipulation functions. As he already says in the comments, this is laborious and error-prone, but sometimes, esp. if the XML is not really XML after unescaping, but something like XML, then this may be your only option.

这篇关于如何解析CDATA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆