如何从 PHP 中的 XML 文件中获取属性的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1256796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:45:47  来源:igfitidea点击:

How to get the value of an attribute from XML file in PHP?

phpxmlsimplexml

提问by Matt

Sorry if this seems like an easy question, but I've started pulling hair out on this...

对不起,如果这似乎是一个简单的问题,但我已经开始在这个问题上拉头发了......

I have a XML file which looks like this...

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

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

I'm trying to get the VarNum.

我正在尝试获取VarNum

So far I've been successful using the follow code to get the other information:

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

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

I just can't get VarNum (the attribute value I think?)

我只是无法获得 VarNum(我认为的属性值?)

Thanks!

谢谢!

回答by zombat

You should be able to get this using SimpleXMLElement::attributes()

你应该能够使用SimpleXMLElement::attributes()

Try this:

尝试这个:

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

That will show you all the name/value attributes for the first fooelement. It's an associative array, so you can do this as well:

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

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

回答by Pascal MARTIN

What about using $xml['VarNum']?

怎么用$xml['VarNum']

Like this :

像这样 :

$str = <<<XML
<VAR VarNum="90">
  <option>1</option>
</VAR>
XML;

$xml=simplexml_load_string($str);
$option=$xml->option;

var_dump((string)$xml['VarNum']);

(I've used simplexml_load_stringbecause I've pasted your XML into a string, instead of creating a file ; what you are doing with simplexml_load_fileis fine, in your case !)

(我使用过simplexml_load_string是因为我已将您的 XML 粘贴到一个字符串中,而不是创建一个文件;就您而言,您正在做的事情simplexml_load_file很好!)

Will get you

会得到你

string '90' (length=2)

With simpleXML, you access attributes with an array syntax.
And you have to cast to a string to get the value, and not and instance of SimpleXMLElement

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

For instance, see example #5of Basic usagein the manual :-)

例如,请参阅手册中基本用法的示例 #5:-)

回答by sunnykachwala

[0] => Array
                (
                    [@attributes] => Array
                        (
                            [uri] => https://abcd.com:1234/abc/cst/2/
                        [id] => 2
                    )

                [name] => Array
                    (
                        [first] => abcd
                        [last] => efg
                    )

                [company] => abc SOLUTION
                [email] => [email protected]
                [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/
                            )

                    )

            )

I applied the below code

我应用了下面的代码

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

but it is not use full i want all the data in single array in php

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