PHP,XML - 获取子节点及其属性 [英] PHP, XML - Get child nodes and their attributes

查看:543
本文介绍了PHP,XML - 获取子节点及其属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从此XML文件中获取所有座位子节点及其属性?

How do i get all the "seat" child nodes and their attributes from this XML file?

   <seatmap id="1">
      <seat row="A" seatnum="01" available="1" />
      <seat row="A" seatnum="02" available="1" />
      <seat row="A" seatnum="03" available="1" />
      <seat row="A" seatnum="04" available="1" />
      <seat row="A" seatnum="05" available="1" />
    </seatmap>

我有不同的座位地图,所以我想通过查询ID
将所有的座位节点及其属性分配给变量。

I have different seatmaps, so i want to get them by querying with an ID then assigning all the 'seat' nodes and their attributes to variables.

我一直在使用DOM方法,但也许simpleXML或XPath会更容易,因为它真的$

I've been using DOM methods so far, but maybe simpleXML or XPath would be easier as its really confusing as you drill down from DOMDocumet, DOMElement, DOMNode.

任何帮助将是伟大的,欢呼!

Any help would be great, cheers!

推荐答案

$XML = <<<XML
<parent>
   <seatmap id="1">
      <seat row="A" seatnum="01" available="1" />
      <seat row="A" seatnum="02" available="1" />
      <seat row="A" seatnum="03" available="1" />
      <seat row="A" seatnum="04" available="1" />
      <seat row="A" seatnum="05" available="1" />
    </seatmap>
</parent>
XML;

$xml_nodes = new SimpleXMLElement($XML);

$nodes = $xml_nodes->xpath('//seatmap[@id = "1"]/seat'); // Replace the ID value with whatever seatmap id you're trying to access

foreach($nodes as $seat)
{
    // You can then access: $seat['row'], $seat['seatnum'], $seat['available']
}

这篇关于PHP,XML - 获取子节点及其属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆