使用php获取xml主要元素的属性

时间:2012-08-03 00:49:31

标签: php xml simplexml

以下是代码

rates.xml

<?xml version="1.0" encoding="UTF-8"?>
<main>
     <unittype id="24944">
          <date value="2020-10-10">
               <baserate>45.50</baserate>
               <closed>0</closed>
               <unitsavailable>2</unitsavailable>
          </date>
     </unittype>
     <unittype id="24944">
          <date value="2020-10-11" value2="2020-10-13">
               <baserate>99</baserate>
               <minimumstay>2</minimumstay>
               <unitsavailable>9</unitsavailable>
          </date>
          <date from="2020-10-15" to="2020-10-20">
                <closed>1</closed>
          </date>
     </unittype>
     <unittype id="24946">
          <date value="2020-10-10">
               <unitsavailable>0</unitsavailable>
               <closed>1</closed>
          </date>
     </unittype>
</main>

这是index.php

<?php
$xml = simplexml_load_file("xml_files/rates.xml");
//$xml = new SimpleXMLElement($xml);
$room_id = 24944;
$date = "2020101";
$hotel = $xml -> xpath('/main/unittype[@id = "'.$room_id.'"]/date[number(translate(@value,\'-\',\'\')) >"' .$date.'" ]');
var_dump($hotel);

foreach($hotel as $room) {
    //var_dump($room);
    echo "<br /><br />";
    echo "The room with the ID: {$room->unittype} <br />";
    echo "Date: {$room[0]->attributes()['value']}<br />";
    echo "Room Price: {$room->baserate}<br />
    Room min stay: {$room->unitsavailable}<br />";
}
?>

现在我正在努力,因为你看到单位类型中的id与被选中的房间一样,因为$ room-&gt; unittype但当然它不起作用所以有人知道如何解决这个问题吗?

var_dump($ hotel)的输出是

array (size=2)
  0 => 
    object(SimpleXMLElement)[2]
      public '@attributes' => 
        array (size=1)
          'value' => string '2020-10-10' (length=10)
      public 'baserate' => string '45.50' (length=5)
      public 'closed' => string '0' (length=1)
      public 'unitsavailable' => string '2' (length=1)
  1 => 
    object(SimpleXMLElement)[3]
      public '@attributes' => 
        array (size=2)
          'value' => string '2020-10-11' (length=10)
          'value2' => string '2020-10-13' (length=10)
      public 'baserate' => string '99' (length=2)
      public 'minimumstay' => string '2' (length=1)
      public 'unitsavailable' => string '9' (length=1)

2 个答案:

答案 0 :(得分:1)

$room->unittype->attributes()->id;

Reference

答案 1 :(得分:0)

我知道了。非常感谢和抱歉,如果我困扰任何人,这里是解决方案以防其他人需要它

<?php
$xml = simplexml_load_file("xml_files/rates.xml");
//$xml = new SimpleXMLElement($xml);
$room_id = 24944;
$date = "2020101";
$hotel = $xml -> xpath('/main/unittype[@id = "'.$room_id.'"]/date[number(translate(@value,\'-\',\'\')) >"' .$date.'" ]');
var_dump($hotel);

foreach($hotel as $room) {
    //var_dump($room);
    echo "<br /><br />";
    $node_name = $room->getName();
    //echo $node_name."<br/><br/>";
    $parent = $room->xpath("parent::*");
    //print_r($parent);
    echo "The room with the ID: {$parent[0]->attributes()['id']} <br />";
    echo "Date: {$room[0]->attributes()['value']}<br />";
    echo "Room Price: {$room->baserate}<br />
    Room min stay: {$room->unitsavailable}<br />";
}
?>