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

时间:2022-10-24 21:15:09

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.

我在找瓦纳姆。

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!

谢谢!

3 个解决方案

#1


23  

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. It's an associative array, so you can do this as well:

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

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

#2


14  

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.
And you have to cast to a string to get the value, and not and instance of SimpleXMLElement

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

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

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

#3


1  

[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中

#1


23  

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. It's an associative array, so you can do this as well:

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

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

#2


14  

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.
And you have to cast to a string to get the value, and not and instance of SimpleXMLElement

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

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

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

#3


1  

[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中