如何在<![CDATA [values]]>中获取值使用PHP的DOM? [英] How to get values inside <![CDATA[values]] > using php DOM?
问题描述
我如何在<![CDATA [values]]>中获取值?使用php DOM. 这是我xml中的一些代码.
How can i get values inside <![CDATA[values]] > using php DOM. This is few code from my 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>
推荐答案
使用PHP DOM非常简单,并且与Javascript DOM非常相似.
Working with PHP DOM is fairly straightforward, and is very similar to Javascript's DOM.
以下是重要的课程:
- DOMNode —在XML/HTML文档中可以遍历的所有内容的基类,包括文本节点,注释节点和 CDATA节点
- DOMElement — 标签的基类.
- DOMDocument —文档的基类.包含加载/保存XML的方法以及普通的DOM文档方法(请参见下文).
- 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).
有一些主要的方法和属性:
There are a few staple methods and properties:
-
DOMDocument->load()
—创建新的DOMDocument
后,请对该对象使用此方法从文件中加载. -
DOMDocument->getElementsByTagName()
—此方法返回具有给定标签名称的文档中所有元素的节点列表.然后,您可以在此列表上进行迭代(foreach
). -
DOMNode->childNodes
—节点的所有子节点的节点列表. (请记住, CDATA 部分是一个节点!) -
DOMNode->nodeType
—获取节点的类型. CDATA节点的类型为 XML_CDATA_SECTION_NODE ,这是一个值为 4 的常量. -
DOMNode->textContent
—获取任何节点的文本内容.
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 CDATA section is a node!)DOMNode->nodeType
— Get the type of 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.
注意:您的CDATA部分格式不正确.我不知道为什么在第一个中有一个额外的]]
,或者在行的末尾有一个未封闭的CDATA部分,但我认为应该只是:
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[Aghia Paraskevi, Skiatos, Greece]]>
将所有内容整合在一起:
- 创建一个新的文档对象并加载XML
- 按标记名称获取所有
Destination
元素并遍历列表 - 遍历每个
Destination
元素的所有子节点 - 检查节点类型是否为
XML_CDATA_SECTION_NODE
- 如果是,请
echo
该节点的textContent
.
- 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.
代码:
$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/>";
}
}
}
结果:
希腊斯基亚托斯的Aghia Paraskevi
西班牙Amettla
希腊Amoliani
德国博布林根
Aghia Paraskevi, Skiatos, Greece
Amettla, Spain
Amoliani, Greece
Boblingen, Germany
这篇关于如何在<![CDATA [values]]>中获取值使用PHP的DOM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!