如何在php中检索xml根元素所有属性?
我有这样一个xml:如何在php中检索xml根元素所有属性?,php,xml,Php,Xml,我有这样一个xml: <ns2:HotelRhg xmlns:ns2="http://v3.abc.somexyz.com/" size="6"> <hotelId>340194</hotelId> <arrivalDate>03/06/2012</arrivalDate> .... 但我只得到ns2=”http://v3.abc.somexyz.com/“而不是尺寸=6 因此,任何人都可以使用PHP解决这个问题。Simplexml
<ns2:HotelRhg xmlns:ns2="http://v3.abc.somexyz.com/" size="6">
<hotelId>340194</hotelId>
<arrivalDate>03/06/2012</arrivalDate>
....
但我只得到ns2=”http://v3.abc.somexyz.com/“而不是尺寸=6
因此,任何人都可以使用PHP解决这个问题。
Simplexml\u load\u string()
是解决方案
$string = <<<XML
<ns2:HotelRhg xmlns:ns2="http://v3.abc.somexyz.com/" size="6">
<hotelId>340194</hotelId>
<arrivalDate>03/06/2012</arrivalDate>
XML;
$xml = simplexml_load_string($string);
foreach($xml->ns2[1]->attributes() as $a => $b) {
echo $a,'="',$b,"\"\n";
$string=$b){
回显$a,“=”,$b,“\”\n”;
如果您只是想要大小,那么请执行以下操作,而不是迭代
$attributes = $xml->ns2[0]->attributes(); //collection of all attributes in ns2
<b><?=$attributes['size'] ?></b> // in your html
$attributes=$xml->ns2[0]->attributes();//ns2中所有属性的集合
//在html中
您真的希望getDocNamespaces
返回名称空间以外的内容吗?这意味着:调用非对象上的成员函数attributes()。如果调用->子节点的属性,则可以获取,但使用->根元素的属性()时,无法获取。
$attributes = $xml->ns2[0]->attributes(); //collection of all attributes in ns2
<b><?=$attributes['size'] ?></b> // in your html