PHP5:在子节点列表中迭代(相当于child_nodes())?

PHP5:在子节点列表中迭代(相当于child_nodes())?,php,xml,Php,Xml,全部, 我有一个XML文档,看起来像这样: <root> <profile> <childA> <childB> <childC> <profile> <blah> <blah> <foo> <bar> <root> $doc = new DomDocument();

全部,

我有一个XML文档,看起来像这样:

<root>
    <profile>
        <childA>
        <childB>
        <childC>
    <profile>
    <blah>
    <blah>
    <foo>
    <bar>
<root>
$doc = new DomDocument();
$doc->loadXML(file_get_contents("php://input"));
$profile_node = $doc->getElementsByTagName("profile")->item(0);
$childnodes = $profile_node->child_nodes();
foreach ($childnodes as $node) {
    // do something with this node
}
到目前为止,一切顺利$profile_节点具有我想要的内容

在PHP4中,我想你会这样做:

<root>
    <profile>
        <childA>
        <childB>
        <childC>
    <profile>
    <blah>
    <blah>
    <foo>
    <bar>
<root>
$doc = new DomDocument();
$doc->loadXML(file_get_contents("php://input"));
$profile_node = $doc->getElementsByTagName("profile")->item(0);
$childnodes = $profile_node->child_nodes();
foreach ($childnodes as $node) {
    // do something with this node
}
但是,我在PHP5中找不到与child_nodes()等价的节点

因为我对PHP几乎一无所知,所以我非常欣赏一个代码示例,这样我就可以看到确切的语法。

根据,DomNode类有一个公共$childNodes变量。您可以直接访问它:

foreach ($profile_node->childNodes as $node) {
    // do something with this node
}

如果您只需要阅读,我建议您使用以下软件包:


SimpleXML包和DOM包是可互换的:您可以使用.

Zed将SimpleXML导入DOM对象-感谢您的回复!很抱歉,我应该提到我对PHP一点也不了解。因此,我不知道创建循环的语法,循环将遍历子节点。您能否提供一个代码示例(例如,使用foreach循环)?如果没有,不用担心。您可以用给定的foreach循环替换foreach循环,并删除它正上方的行。正确。这完全取代了下面的代码“在PHP4中,我想你会这样做”只是一个注释-我把上面我自己的代码搞砸了。它应该改为“$profile\u节点=$doc->getElementsByTagName(“profile”);”,而不是“$profile\u节点=$doc->getElementsByTagName(“profile”)->item(0);”