【发布时间】:2012-04-07 18:53:36
【问题描述】:
我有一堆 XML 文件,我正在使用 XMLReader 将它们加载到我的脚本中,创建 DOM 对象,然后转换为 Simplexml。
问题是 XML 文件之一使用 SIMPLEXML 忽略的 CDATA,通常使用 SIMPLEXML_LOAD_FILE 我会添加 LIBXML_NOCDATA 参数,但由于我使用的是 simplexml_import_dom,我无法弄清楚如何在下面的场景中忽略 CDATA。
有什么想法吗?
非常感谢 布雷特
$file = 'test.xml';
$reader = new XMLReader();
$reader->open($file);
while ($reader->read())
{
// are we in a product?
if ($reader->nodeType == XMLReader::ELEMENT &&
strtolower($reader->localName) == 'product')
{
if (!$node = $reader->expand()) {
//do nothing
}
else {
// expand the node into a DOMNode
// Convert to SimpleXML via DOM, messy but SimpleXML is soo much nicer.
$dom = new DomDocument();
$dom->appendChild($dom->importNode($node, true));
$products = simplexml_import_dom($dom);
// do whatever we want to do with the product data
}
【问题讨论】: