1

我有一个用 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 的限制吗?还是我做错了什么?

问候

4

1 回答 1

0

你的问题不是很清楚,但可能需要这样的东西:

$xmlDoc = new SimpleXMLElement($docStr);
$allNamespaces = $xmlDoc->getNamespaces(true);
foreach ($allNamespaces as $nspace => $nurl) {
      $xmlDoc->registerXPathNamespace($nspace, $nurl);
}

阅读 SimpleXML 和命名空间。

这几乎可以肯定是解决您的问题的方法。见这里: http: //blog.sherifmansour.com/ ?p=302

于 2012-10-22T23:27:59.163 回答