如何使用 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
How to get values inside <![CDATA[values]] > using php DOM?
提问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 节点
- DOMElement—tags的基类。
- DOMDocument— 文档的基类。包含加载/保存 XML 的方法,以及普通的 DOM 文档方法(见下文)。
There are a few staple methods and properties:
有一些主要的方法和属性:
DOMDocument->load()
— After creating a newDOMDocument
, use this method on that object to load from a file.DOMDocument->getElementsByTagName()
— this method returns a node list of all elements in the document with the given tag name. Then you can iterate (foreach
) on this list.DOMNode->childNodes
— A node list of all children of a node. (Remember, a CDATAsection is a node!)DOMNode->nodeType
— Get the typeof a node. CDATA nodes have type XML_CDATA_SECTION_NODE, which is a constant with the value 4.DOMNode->textContent
— get the text content of any node.
DOMDocument->load()
— 创建新的 后DOMDocument
,在该对象上使用此方法从文件加载。DOMDocument->getElementsByTagName()
— 此方法返回文档中具有给定标签名称的所有元素的节点列表。然后您可以foreach
在此列表上迭代 ( )。DOMNode->childNodes
— 一个节点的所有子节点的节点列表。(请记住,CDATA部分是一个节点!)DOMNode->nodeType
— 获取节点的类型。CDATA 节点的类型为XML_CDATA_SECTION_NODE,它是一个值为4的常量。DOMNode->textContent
— 获取任意节点的文本内容。
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:
把这一切放在一起,我们:
- Create a new document object and load the XML
- Get all
Destination
elements by tag name and iterate over the list - Iterate over all child nodes of each
Destination
element - Check if the node type is
XML_CDATA_SECTION_NODE
- If it is,
echo
thetextContent
of that node.
- 创建一个新的文档对象并加载 XML
Destination
按标签名称获取所有元素并遍历列表- 迭代每个
Destination
元素的所有子节点 - 检查节点类型是否为
XML_CDATA_SECTION_NODE
- 如果是,
echo
在textContent
那个节点。
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.
希望这对你有用。