我有这个XML文件:
<images>
<photo image="images/101.jpg" colorboxImage="images/101.jpg" colorboxInfo="Item 01" colorboxClass="image" url = "http://www.flashxml.net" target="_blank">
<![CDATA[<head>Hello</head><body>Welcome to the new Circular Gallery</body>]]></photo>
<photo image="images/102.jpg" colorboxImage="images/102.jpg" colorboxInfo="Item 02" colorboxClass="image" url = "http://www.flashxml.net" target="_blank">
<![CDATA[<head>Download the new Circular Gallery</head><body>for FREE</body>]]></photo>
<photo image="images/103.jpg" colorboxImage="images/103.jpg" colorboxInfo="Item 03" colorboxClass="image" url = "http://www.flashxml.net" target="_blank">
<![CDATA[<head>Insert it in your website</head><body>without any special skills or software</body>]]></photo>
<photo image="images/104.jpg" colorboxImage="images/104.jpg" colorboxInfo="Item 04" colorboxClass="image" url = "http://www.flashxml.net" target="_blank">
<![CDATA[<head>Put your own images in the "images" folder</head><body>and update the images.xml file accordingly</body>]]></photo>
<photo image="images/105.jpg" colorboxImage="images/105.jpg" colorboxInfo="Item 05" colorboxClass="image" url = "http://www.flashxml.net" target="_blank">
<![CDATA[<head>You can put <a href="http://www.flashxml.net" target="_blank">links</a> in this HTML/CSS formatted text</head><body>and you can also put links behind the images</body>]]></photo></images>
和这个PHP代码:
$xml = simplexml_load_file("images.xml");
foreach ($xml->children() as $child)
{
echo "Child node: " . $child . "<br />" . $child["image"] . "<br />";
}
我得到的结果如下:
Child node: HelloWelcome to the new Circular Gallery
images/101.jpg
Child node: Download the new Circular Galleryfor FREE
images/102.jpg
Child node: Insert it in your websitewithout any special skills or software
images/103.jpg
我想分开这一部分的文字:
<![CDATA[<head>You can put <a href="http://www.flashxml.net" target="_blank">links</a> in this HTML/CSS formatted text</head><body>and you can also put links behind the images</body>]]>
我想阅读头部和身体部分的文字,但是这段代码将它们合并为一个文本,我不知道如何获取每一个并做我需要做的事情
答案 0 :(得分:0)
看起来您必须再次解析节点内容(使用simplexml_load_string)。但我发现SimpleXMLElement中没有函数来检索节点的文本内容,只检索子元素。奇怪。
但看起来转换为节点的字符串会返回此节点的内容,因此您可以尝试这样做:
$xml = simplexml_load_file("images.xml");
foreach ($xml->children() as $child)
{
echo "Child node: " . $child . "<br />" . $child["image"] . "<br />";
$content = simplexml_load_string("<dummy>". $child . "</dummy>");
echo "child head: " . $content->head . "<br/>";
echo "child body: " . $content->body . "<br/>";
}