在php中从头到尾读取xml文件

发布于 2024-11-29 08:39:27 字数 943 浏览 1 评论 0原文

我想从头开始读取 xml 文件,假设我的 xml 是这样的:

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
<note>
<to>Samy</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't Miss me this weekend!</body>
</note>

我需要先阅读最后一条注释,我使用这样的代码,

$x = $xmldoc->getElementsByTagName('note');
        $nofnews = $xmldoc->getElementsByTagName('note')->length;
        for ($i = 0; $i < $nofnews; $i++) {
            $item_title = $x->item($i)->getElementsByTagName('to')
                            ->item(0)->nodeValue;
            $item_link = $x->item($i)->getElementsByTagName('from')
                            ->item(0)->nodeValue;
                            }

提前谢谢您

i want to read xml file from end to start suppose my xml like that :

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
<note>
<to>Samy</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't Miss me this weekend!</body>
</note>

i need to read last note first i use code like that

$x = $xmldoc->getElementsByTagName('note');
        $nofnews = $xmldoc->getElementsByTagName('note')->length;
        for ($i = 0; $i < $nofnews; $i++) {
            $item_title = $x->item($i)->getElementsByTagName('to')
                            ->item(0)->nodeValue;
            $item_link = $x->item($i)->getElementsByTagName('from')
                            ->item(0)->nodeValue;
                            }

thank you in advance

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

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

发布评论

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

评论(2

听,心雨的声音 2024-12-06 08:39:27

getElementsByTagName 返回一个 DOMNodeList< /a>.您可以像您已经做的那样通过每个元素的数字索引(0 到 length-1)访问每个元素,但只是从末尾开始,而不是 for 循环的开头,然后向下计数:

$x = $xmldoc->getElementsByTagName('note');
$nofnews = $x->length;
for ($i = $nofnews-1; $i > -1; $i--)
{
    $item = $x->item($i);
    $item_title = $item->getElementsByTagName('to')
                        ->item(0)->nodeValue;
    $item_link = $item->getElementsByTagName('from')
                        ->item(0)->nodeValue;
}

这应该已经为您提供了。

getElementsByTagName returns a DOMNodeList. You can access each element by it's numerical index (0 to length-1) as you already do, but just start at the end, not the beginning in your for loop, then count downwards:

$x = $xmldoc->getElementsByTagName('note');
$nofnews = $x->length;
for ($i = $nofnews-1; $i > -1; $i--)
{
    $item = $x->item($i);
    $item_title = $item->getElementsByTagName('to')
                        ->item(0)->nodeValue;
    $item_link = $item->getElementsByTagName('from')
                        ->item(0)->nodeValue;
}

This should already to it for you.

暮凉 2024-12-06 08:39:27

我不确定我是否正确理解你的问题,但你似乎想开始阅读
XML 文档的根元素的子元素按相反顺序排列?

(您提供的示例内容也取自此处: http://www.w3schools.com /PHP/php_xml_simplexml.asp)为什么不效仿他们的例子呢?

但无论如何,这应该可以解决问题:

<?php
$xml = simplexml_load_file("test.xml");

echo $xml->getName() . "<br />";

foreach(array_reverse($xml->children()) as $child)
  {
  // process your child element in here
  }
?>

-->使用 array_reverse() 函数:http://www.w3schools.com/php/func_array_reverse。 ASP

I am not sure that I understand your question correctly but it seems you want to start reading
the XML document's root element's children in reverse order?

(Also the example content you're giving is taken from here: http://www.w3schools.com/PHP/php_xml_simplexml.asp) why not follow their example?

But anyway, this should do the trick:

<?php
$xml = simplexml_load_file("test.xml");

echo $xml->getName() . "<br />";

foreach(array_reverse($xml->children()) as $child)
  {
  // process your child element in here
  }
?>

--> make use of the array_reverse() function: http://www.w3schools.com/php/func_array_reverse.asp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文