SimpleXML:获取子节点

发布于 2024-11-27 08:40:03 字数 870 浏览 0 评论 0原文

<?xml version="1.0" encoding="utf-8" ?> 
<items>
<aaa>
</aaa>
<aaa>
</aaa>
<aaa>
    <bbb>
    </bbb>
    <bbb>
    </bbb>
    <bbb>
        <ccc>
            Only this childnod what I need
        </ccc>
        <ccc>
            And this one
        </ccc>
    </bbb>
</aaa>
</items>

我想用 PHP 解析上面给出的 XML。我不知道如何获取这两个子节点。

当我使用下面的代码时,它会触发错误: Warning: main() [function.main]: Cannot add element bbb number 2 when only 0 such elements contains in

<?php
header('Content-type:text/html; charset=utf-8');
$xml = simplexml_load_file('2.xml');
    foreach ($xml->aaa[2]->bbb as $bbb){
        echo $bbb[2]->$ccc; // wrong in here
        echo $bbb[3]->$ccc;
    }
?>
<?xml version="1.0" encoding="utf-8" ?> 
<items>
<aaa>
</aaa>
<aaa>
</aaa>
<aaa>
    <bbb>
    </bbb>
    <bbb>
    </bbb>
    <bbb>
        <ccc>
            Only this childnod what I need
        </ccc>
        <ccc>
            And this one
        </ccc>
    </bbb>
</aaa>
</items>

I want parse the XML as given above with PHP. I don't know how to get these two child nodes.

When I use the code below, it triggers the error: Warning: main() [function.main]: Cannot add element bbb number 2 when only 0 such elements exist in

<?php
header('Content-type:text/html; charset=utf-8');
$xml = simplexml_load_file('2.xml');
    foreach ($xml->aaa[2]->bbb as $bbb){
        echo $bbb[2]->$ccc; // wrong in here
        echo $bbb[3]->$ccc;
    }
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紫﹏色ふ单纯 2024-12-04 08:40:03

使用 xpath

$ccc = $xml->xpath('aaa/bbb/ccc');
foreach($ccc as $c){
    echo (string)$c."<br/>";
}

结果:

Only this childnod what I need 
And this one 

另外,在您最初的尝试中,$bbb[ 3] 没有意义,因为您只有三个项目,从索引 0 开始。

Use xpath

$ccc = $xml->xpath('aaa/bbb/ccc');
foreach($ccc as $c){
    echo (string)$c."<br/>";
}

Result:

Only this childnod what I need 
And this one 

Also, in your initial attempt, $bbb[3] does not make sense because you have three items only, starting with index 0.

梦过后 2024-12-04 08:40:03

我不知道如何使用 simplexml 的返回,但从我从你的代码中看到的,这应该有效:

<?php
header('Content-type:text/html; charset=utf-8');
$xml = simplexml_load_file('2.xml');
echo $xml->aaa[2]->bbb[2]->ccc[0];
echo $xml->aaa[2]->bbb[2]->ccc[1];
?> 

I don't know how to use the return of simplexml, but from what I have seen from your code, this should work:

<?php
header('Content-type:text/html; charset=utf-8');
$xml = simplexml_load_file('2.xml');
echo $xml->aaa[2]->bbb[2]->ccc[0];
echo $xml->aaa[2]->bbb[2]->ccc[1];
?> 
辞别 2024-12-04 08:40:03

更动态的解决方案

<?php
header('Content-type:text/html; charset=utf-8');
$xml = simplexml_load_file('2.xml');
    foreach ($xml->aaa as $aaa){
        if ($aaa->bbb) {
            foreach ($aaa->bbb as $bbb) {
                if ($bbb->ccc)
                    foreach ($bbb->ccc as $ccc) {
                        echo $ccc;
                    }
                }
            }
        }
    }
?> 

A more dynamic solution

<?php
header('Content-type:text/html; charset=utf-8');
$xml = simplexml_load_file('2.xml');
    foreach ($xml->aaa as $aaa){
        if ($aaa->bbb) {
            foreach ($aaa->bbb as $bbb) {
                if ($bbb->ccc)
                    foreach ($bbb->ccc as $ccc) {
                        echo $ccc;
                    }
                }
            }
        }
    }
?> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文