从解析的 XML 中读取数据

发布于 2024-12-11 14:06:58 字数 320 浏览 0 评论 0原文

我有一个由 API 返回的 XML 文件。我想从中获取特定数据。我已通过 $xml = simpleXMLToArray(simplexml_load_string($response)); 将 XML 转换为数组 $response 是 XML。$xml 是包含所有 XML 数据的数组。我想从中提取数据,但它不起作用。

foreach($xml['conetent']['album']['media'] as $media)
    {
        //var_dump($media);
      echo $media;
    }

I have an XML file that is returned by an API.i want to get specfic Data out of it.I had converted the XML into array by $xml = simpleXMLToArray(simplexml_load_string($response));
$response is the XML.$xml is the array that have all the XML data.I want to extract data out of it but its not working.

foreach($xml['conetent']['album']['media'] as $media)
    {
        //var_dump($media);
      echo $media;
    }

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

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

发布评论

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

评论(2

内心荒芜 2024-12-18 14:06:58

一旦您将“conetent”的拼写更正为“content”,它在这里就可以正常工作

编辑:

因此,根据您的问题的更新,这样怎么样:

foreach ($xml->xpath('//content/album') as $album)
{
    print "Album: ";
    foreach($album->attributes() as $key => $value)
        print "\t".$key." = ".$value."\n";
    foreach($album->xpath('album') as $subalbum)
    {
        print "\tSub album:\n";
        foreach($subalbum->attributes() as $key => $value)
            print "\t\t".$key." = ".$value."\n";
    }
}

It works fine here once you correct the spelling of 'conetent' to 'content'

Edit:

So with the update from your question, how about this:

foreach ($xml->xpath('//content/album') as $album)
{
    print "Album: ";
    foreach($album->attributes() as $key => $value)
        print "\t".$key." = ".$value."\n";
    foreach($album->xpath('album') as $subalbum)
    {
        print "\tSub album:\n";
        foreach($subalbum->attributes() as $key => $value)
            print "\t\t".$key." = ".$value."\n";
    }
}
爱你不解释 2024-12-18 14:06:58

看起来您有两个级别的专辑标签。不使用 simpleXMLToArray() 可能会更容易,而只需使用 xpath:

$result= $xml->xpath('//media');

while(list( , $node) = each($result)) {
    echo '/media: ',$node,"\n";
}

It would appear you have two levels of album tags. It would probably be easier not to use simpleXMLToArray() but just search for your media with xpath:

$result= $xml->xpath('//media');

while(list( , $node) = each($result)) {
    echo '/media: ',$node,"\n";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文