【问题标题】:How to get the value of an attribute from XML file in PHP?如何从 PHP 中的 XML 文件中获取属性的值?
【发布时间】:2010-11-18 09:54:27
【问题描述】:

很抱歉,这似乎是一个简单的问题,但我已经开始在这个问题上大发雷霆了……

我有一个看起来像这样的 XML 文件...

<VAR VarNum="90">
  <option>1</option>
</VAR>

我正在尝试获取 VarNum

到目前为止,我已经成功使用以下代码获取其他信息:

$xml=simplexml_load_file($file);
$option=$xml->option;

我就是无法获取 VarNum(我认为的属性值?)

谢谢!

【问题讨论】:

  • 是的,这是属性的值。

标签: php xml simplexml


【解决方案1】:

试试这个

$xml->attributes()['YourPropertyName']; //check property case also

【讨论】:

    【解决方案2】:
    [0] => Array
                    (
                        [@attributes] => Array
                            (
                                [uri] => https://abcd.com:1234/abc/cst/2/
                            [id] => 2
                        )
    
                    [name] => Array
                        (
                            [first] => abcd
                            [last] => efg
                        )
    
                    [company] => abc SOLUTION
                    [email] => abc@xyz.com
                    [homepage] => WWW.abcxyz.COM
                    [phone_numbers] => Array
                        (
                            [phone_number] => Array
                                (
                                    [0] => Array
                                        (
                                            [main] => true
                                            [type] => work
                                            [list_order] => 1
                                            [number] => +919876543210
                                        )
    
                                    [1] => Array
                                        (
                                            [main] => false
                                            [type] => mobile
                                            [list_order] => 2
                                            [number] => +919876543210
                                        )
    
                                )
    
                        )
    
                    [photo] => Array
                        (
                            [@attributes] => Array
                                (
                                    [uri] => https://abcd.com:1234/abc/cst/2/cust_photo/
                                )
    
                        )
    
                )
    

    我应用了下面的代码

    $xml = simplexml_load_string($response);
    $json = json_encode($xml);
    $array = json_decode($json,TRUE);
    print_r($array);
    

    但它没有使用完整我想要 php 中单个数组中的所有数据

    【讨论】:

      【解决方案3】:

      你应该可以使用SimpleXMLElement::attributes()得到这个

      试试这个:

      $xml=simplexml_load_file($file);
      foreach($xml->Var[0]->attributes() as $a => $b) {
          echo $a,'="',$b,"\"\n";
      }
      

      这将显示第一个 foo 元素的所有名称/值属性。它是一个关联数组,所以你也可以这样做:

      $attr = $xml->Var[0]->attributes();
      echo $attr['VarNum'];
      

      【讨论】:

      • 嗨。谢谢回复。当我尝试这个时,我收到以下错误 - “致命错误:调用非对象上的成员函数属性()”
      • 谢谢!我能够得到这个工作(这是一个语法错误 - doh!)再次感谢!
      【解决方案4】:

      使用$xml['VarNum'] 怎么样?

      像这样:

      $str = <<<XML
      <VAR VarNum="90">
        <option>1</option>
      </VAR>
      XML;
      
      $xml=simplexml_load_string($str);
      $option=$xml->option;
      
      var_dump((string)$xml['VarNum']);
      

      (我使用了simplexml_load_string,因为我已将您的 XML 粘贴到一个字符串中,而不是创建一个文件;在您的情况下,您使用 simplexml_load_file 所做的一切都很好!)

      会得到你

      string '90' (length=2)
      

      使用 simpleXML,您可以使用数组语法访问属性。
      并且您必须转换为字符串才能获取值,而不是 SimpleXMLElement 的实例

      例如,请参阅手册中Basic usage示例#5 :-)

      【讨论】:

      • 注意:示例中转换为(字符串)。
      • @null :(好名字,顺便说一句^^):谢谢!我编辑添加了这一点(以及更多的精度)
      • 在访问节点的属性时,转换为字符串非常重要。当我们未能包含演员表时,我们看到了一些时髦的行为(空值)。
      猜你喜欢
      • 1970-01-01
      • 2020-08-18
      • 1970-01-01
      • 1970-01-01
      • 2023-01-10
      • 1970-01-01
      • 2013-05-14
      • 1970-01-01
      相关资源
      最近更新 更多