如何使用 php DOM 在 <![CDATA[values]]> 中获取值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6674322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:58:06  来源:igfitidea点击:

How to get values inside <![CDATA[values]] > using php DOM?

phpxml

提问by Hearaman

How can i get values inside <![CDATA[values]] > using php DOM. This is few code from my xml.

如何使用 php DOM 在 <![CDATA[values]]> 中获取值。这是我的 xml 中的一些代码。

     <Destinations>

        <Destination>
            <![CDATA[Aghia Paraskevi, Skiatos, Greece]]>
            <CountryCode>GR</CountryCode>
        </Destination>

        <Destination>
            <![CDATA[Amettla, Spain]]>
            <CountryCode>ES</CountryCode>
        </Destination>

        <Destination>
            <![CDATA[Amoliani, Greece]]>
            <CountryCode>GR</CountryCode>
        </Destination>

        <Destination>
            <![CDATA[Boblingen,  Germany]]>
            <CountryCode>DE</CountryCode>
        </Destination>

  </Destinations>

回答by Nicole

Working with PHP DOM is fairly straightforward, and is very similar to Javascript's DOM.

使用 PHP DOM 相当简单,并且与 Javascript 的 DOM 非常相似。

Here are the important classes:

以下是重要的类:

  • DOMNode— The base class for anything that can be traversed inside an XML/HTML document, including text nodes, comment nodes, and CDATA nodes
  • DOMElement— The base class for tags.
  • DOMDocument— The base class for documents. Contains the methods to load/save XML, as well as normal DOM document methods (see below).
  • DOMNode— 可以在 XML/HTML 文档中遍历的任何内容的基类,包括文本节点、注释节点和CDATA 节点
  • DOMElementtags的基类。
  • DOMDocument— 文档的基类。包含加载/保存 XML 的方法,以及普通的 DOM 文档方法(见下文)。

There are a few staple methods and properties:

有一些主要的方法和属性:

Note: Your CDATA sections are malformed. I don't know why there is an extra ]]in the first one, or an unclosed CDATA section at the end of the line, but I think it should simply be:

注意:您的 CDATA 部分格式不正确。我不知道为什么]]第一个中有一个额外的部分,或者行尾有一个未关闭的 CDATA 部分,但我认为它应该只是:

<![CDATA[Aghia Paraskevi, Skiatos, Greece]]>

Putting this all together we:

把这一切放在一起,我们:

  1. Create a new document object and load the XML
  2. Get all Destinationelements by tag name and iterate over the list
  3. Iterate over all child nodes of each Destinationelement
  4. Check if the node type is XML_CDATA_SECTION_NODE
  5. If it is, echothe textContentof that node.
  1. 创建一个新的文档对象并加载 XML
  2. Destination按标签名称获取所有元素并遍历列表
  3. 迭代每个Destination元素的所有子节点
  4. 检查节点类型是否为 XML_CDATA_SECTION_NODE
  5. 如果是,echotextContent那个节点。

Code:

代码:

$doc = new DOMDocument();
$doc->load('test.xml');
$destinations = $doc->getElementsByTagName("Destination");
foreach ($destinations as $destination) {
    foreach($destination->childNodes as $child) {
        if ($child->nodeType == XML_CDATA_SECTION_NODE) {
            echo $child->textContent . "<br/>";
        }
    }
}

Result:

结果:

Aghia Paraskevi, Skiatos, Greece
Amettla, Spain
Amoliani, Greece
Boblingen, Germany

圣帕拉斯凯维,Skiatos,希腊
Amettla,西班牙
阿莫利亚尼岛,希腊
伯布林根,德国

回答by Grzegorz Brz?czyszczykiewicz

Use this:

用这个:

$parseFile = simplexml_load_file($myXML,'SimpleXMLElement', LIBXML_NOCDATA)

and next :

接下来:

foreach ($parseFile->yourNode as $node ){
etc...
}

回答by Aniket Singh

Best and easy way

最好和简单的方法

$xml = simplexml_load_string($xmlData, 'SimpleXMLElement', LIBXML_NOCDATA);
$xmlJson = json_encode($xml);
$xmlArr = json_decode($xmlJson, 1); // Returns associative array

回答by Brijesh Singh

Use replace CDATA before parsing PHP DOM element after that you can get the innerXml or innerHtml:

在解析PHP DOM元素之前使用replace CDATA,之后您可以获得innerXml或innerHtml:

str_replace(array('<\![CDATA[',']]>'), '', $xml);

回答by Adnan Ahmad

I use following code. Its not only read all xml data with

我使用以下代码。它不仅可以读取所有 xml 数据

<![CDATA[values]] > 

but also convert xml object to php associative array. So we can apply loop on the data.

但也将 xml 对象转换为 php 关联数组。所以我们可以对数据应用循环。

$xml_file_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA),true), true);

Hope this will work for you.

希望这对你有用。