我有一个用 PHP SimpleXML 解析的 XML。我的 XML 文件的某些部分是这样的:
<deviceStatusPolling interval="60">
<r:datapoint programmaticName="t_val_sys_pct_mssDbPartitionUsage" />
</deviceStatusPolling>
当我解析它时,这就是我得到的:
Parsing 'deviceStatusPolling'...
Has 1 attribute(s):
- interval: 60
它不解析孩子:
<r: datapoint programmaticName="t_val_sys_pct_mssDbPartitionUsage" />
这是解析函数:
function parse_recursive(SimpleXMLElement $element, $level = 0) {
$indent = str_repeat("\t", $level); // determine how much we'll indent
$value = trim((string) $element); // get the value and trim any whitespace from the start and end
$attributes = $element->attributes(); // get all attributes
$children = $element->children(); // get all children
echo "{$indent}Parsing '{$element->getName()}'...<br>";
if(count($children) == 0 && !empty($value)) // only show value if there is any and if there aren't any children
{
echo "{$indent}Value: {$element}<br>";
}
// only show attributes if there are any
if(count($attributes) > 0)
{
echo $indent.'Has '.count($attributes).' attribute(s):<br>';
foreach($attributes as $attribute)
{
echo "{$indent}- {$attribute->getName()}: {$attribute}<br>";
}
}
// only show children if there are any
if(count($children))
{
echo $indent.'Has '.count($children).' child(ren):<br>';
foreach($children as $child)
{
parse_recursive($child, $level+1); // recursion :)
}
}
echo $indent; // just to make it "cleaner"
echo "<br>";
}
这是 SimpleXML 的限制吗?还是我做错了什么?
问候