Php simplexml不';不读CDATA
我想从远程位置的xml文件中获取数据,该文件包含所有节点中的CDATA信息,如下所示。 我使用下面的PHP函数获取这些信息,但它不起作用,似乎无法从xml文件中捕获CDATA标记。 问题是我的代码是否正确?如果它是错误的,您可以建议任何php代码来获取请求的信息吗Php simplexml不';不读CDATA,php,xml,cdata,Php,Xml,Cdata,我想从远程位置的xml文件中获取数据,该文件包含所有节点中的CDATA信息,如下所示。 我使用下面的PHP函数获取这些信息,但它不起作用,似乎无法从xml文件中捕获CDATA标记。 问题是我的代码是否正确?如果它是错误的,您可以建议任何php代码来获取请求的信息吗 <Items> <Item ID="1"> <Name>Mountain</Name> <Properties>
<Items>
<Item ID="1">
<Name>Mountain</Name>
<Properties>
<Property Code="feature"><![CDATA[<ul><li>sample text</li></ul>]]></Property>
<Property Code="SystemRequirements"><![CDATA[Windows XP/Windows Vista]]></Property>
<Property Code="Description" Type="plain"><![CDATA[sample text2]]></Property>
</Properties>
</Item>
<Items>
山
示例文本 ]>
这是我的php代码:
<?
function xmlParse($file, $wrapperName, $callback, $limit = NULL) {
$xml = new XMLReader();
if (!$xml->open($file)) {
die("Failed to open input file.");
}
$n = 0;
$x = 0;
while ($xml->read()) {
if ($xml->nodeType == XMLReader::ELEMENT && $xml->name == $wrapperName) {
while ($xml->read() && $xml->name != $wrapperName) {
if ($xml->nodeType == XMLReader::ELEMENT) {
//$subarray[]=$xml->expand();
$doc = new DOMDocument('1.0', 'UTF-8');
$simplexml = simplexml_import_dom($doc->importNode($xml->expand(), true));
$subarray[]=$simplexml;
}
}
if ($limit == NULL || $x < $limit) {
if ($callback($subarray)) {
$x++;
}
unset($subarray);
}
$n++;
}
}
$xml->close();
}
echo '<pre>';
function func1($s) {
print_r($s);
}
xmlParse('myfile.xml', 'Item', 'func1', 100);
使用DOMDocument打开xml文件有多种方法,例如:
$file = "1.xml";
$xml = simplexml_load_file($file);
foreach($xml->Item->Properties->children() as $properties) {
printf("%s", $properties);
}
有多种方法可以使用DOMDocument打开xml文件,例如:
$file = "1.xml";
$xml = simplexml_load_file($file);
foreach($xml->Item->Properties->children() as $properties) {
printf("%s", $properties);
}
把它当作一根绳子
<ul><li>sample text</li></ul>
Windows XP/Windows Vista
sample text2
输出
- 示例文本
Windows XP/Windows Vista
示例文本2
将其视为字符串
<ul><li>sample text</li></ul>
Windows XP/Windows Vista
sample text2
输出
- 示例文本
Windows XP/Windows Vista
示例文本2
您看到这个问题中的信息了吗?-这可能正是你的情况。实际上这是你的情况。不,您不能使用simplexmlelement信任print\r
或var\u dump
的输出。这是在骗你(参考:;)你看到这个问题中的信息了吗这可能正是你的情况。实际上这是你的情况。不,您不能使用simplexmlelement信任print\r
或var\u dump
的输出。这是在骗你(参考:;)