简体   繁体   English

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

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

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!谢谢!

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 foo element.这将显示第一个foo元素的所有名称/值属性。 It's an associative array, so you can do this as well:这是一个关联数组,因此您也可以这样做:

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

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_string because I've pasted your XML into a string, instead of creating a file ; what you are doing with simplexml_load_file is 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.使用 simpleXML,您可以使用数组语法访问属性。
And you have to cast to a string to get the value, and not and instance of SimpleXMLElement并且您必须转换为字符串才能获取值,而不是SimpleXMLElement实例

For instance, see example #5 of Basic usage in the manual :-)例如,请参阅手册中基本用法的示例 #5 :-)

[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/
                            )

                    )

            )

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中单个数组中的所有数据

尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM