PHP DOM XML 最好使用 xPath 读取

发布于 2024-12-18 13:37:59 字数 1281 浏览 0 评论 0原文

我有这个 xml

<App>
<Id id="129"/>
<Path id="category/self"/>
<Screenshots>
    <Screenshot url="small_480x320_34eaefb6c934a0d82da637bec2276bdd_ebecfca09179d055ed4d3cf15a41bf7c-8f95512356608b888f288b374db7b37fabb3ce27.jpg"/>
    <Screenshot url="small_480x320_438a9e1352bda4ef9724ae422473a556_615625ab835b09aa712e9c84c056cd7a-15bc8803724197129aea59577cac334736fadd3f.jpg"/>
    <Screenshot url="small_480x320_ce4d635a786e427983c30f1abd8617a1_4d9b74b7f8ffa9ca19d64bee4a9592b9-36a9e665b0546b09e5496a5d02ba5e1bcd0bf563.jpg"/>
    <Screenshot url="small_480x320_3e95d91de62aa2c52e4ff7f32a10a1d9_8a39c9eb9fa2bc2d5f5e789705713e12-a5ec1865bc88c6569af106d9131b45f1f82f8a1b.jpg"/>
    <Screenshot url="small_480x320_3776b5fc7eceed4da9256105889578a3_4265bb97bda45a213357341ddaa81ce9-dae64333deb2cc09f69d0e74e79f4def199caaa2.jpg"/>
</Screenshots>
</App>

,我想解析它并

$Id = "129";
$Path = "category/self";
$Screenshots = array(... screenshots ... all 5 of them);

现在...我使用 XSLT 来做到这一点没有问题,但在阅读文档后我并不完全理解 DOM。到目前为止我所拥有的就是

这个 xpath 东西 http://codepad.org/BtvNEnjl 它只是读取之间的值标签,我不知道如何让它“foreach”它......

I have this xml

<App>
<Id id="129"/>
<Path id="category/self"/>
<Screenshots>
    <Screenshot url="small_480x320_34eaefb6c934a0d82da637bec2276bdd_ebecfca09179d055ed4d3cf15a41bf7c-8f95512356608b888f288b374db7b37fabb3ce27.jpg"/>
    <Screenshot url="small_480x320_438a9e1352bda4ef9724ae422473a556_615625ab835b09aa712e9c84c056cd7a-15bc8803724197129aea59577cac334736fadd3f.jpg"/>
    <Screenshot url="small_480x320_ce4d635a786e427983c30f1abd8617a1_4d9b74b7f8ffa9ca19d64bee4a9592b9-36a9e665b0546b09e5496a5d02ba5e1bcd0bf563.jpg"/>
    <Screenshot url="small_480x320_3e95d91de62aa2c52e4ff7f32a10a1d9_8a39c9eb9fa2bc2d5f5e789705713e12-a5ec1865bc88c6569af106d9131b45f1f82f8a1b.jpg"/>
    <Screenshot url="small_480x320_3776b5fc7eceed4da9256105889578a3_4265bb97bda45a213357341ddaa81ce9-dae64333deb2cc09f69d0e74e79f4def199caaa2.jpg"/>
</Screenshots>
</App>

and i want to parse it and have

$Id = "129";
$Path = "category/self";
$Screenshots = array(... screenshots ... all 5 of them);

Now ... i have no problem doing this with XSLT for example, but i don't fully understand DOM after reading the documentation. All i have so far is

is this xpath thing http://codepad.org/BtvNEnjl that just read an value between tags and i don't know how to make it to "foreach" it ...

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

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

发布评论

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

评论(1

沙与沫 2024-12-25 13:37:59
$xml = simplexml_load_string($str); // or use simplexml_load_file()

$id = (string) $xml->Id['id'];
$path = (string) $xml->Path['id'];

$screenshots = array();
foreach($xml->xpath('/App/Screenshots/Screenshot') as $screenshot)
{
    $screenshots[] = (string) $screenshot['url'];
}
$xml = simplexml_load_string($str); // or use simplexml_load_file()

$id = (string) $xml->Id['id'];
$path = (string) $xml->Path['id'];

$screenshots = array();
foreach($xml->xpath('/App/Screenshots/Screenshot') as $screenshot)
{
    $screenshots[] = (string) $screenshot['url'];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文