如何使用 simplexml_load_string 在转换后的数组中获取 xml 标签属性
How do I get xml tag attributes in converted array using simplexml_load_string
我有以下 xml 数据。 <question>
中有问题,<answer>
中有所有可能的答案。 <answer>
标签具有 "correct" 属性,这是该问题的正确答案。
所以在这里我试图读取 <answer>
的这个 "Correct" 属性。
在这里,当我使用 "simplexml_load_string" 函数时,它将 xml 转换为 php 数组,但它不会 return 这个 "Correct" 属性。
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<questions>
<question type="1" text="Which one of the following addresses is associated with you?">
<answer correct="false">ABC</answer>
<answer correct="false">PQR</answer>
<answer correct="false">ASD</answer>
<answer correct="false">5374 </answer>
<answer correct="false">8288 SELKIRK</answer>
<answer correct="false">1558 NICHOLS</answer>
<answer correct="true">1400 AMERICAN LN</answer>
<answer correct="false">None of the above</answer>
</Question>
</Questions>
我怎样才能做到这一点?
Xml 区分大小写。如果将 </Question>
和 </Questions>
变成小写,一切正常:
$xml = simplexml_load_string($str);
foreach($xml->xpath('/questions/question/answer') as $ans)
echo $ans['correct'] .' : ' . $ans . "\n";
结果:
false : ABC
false : PQR
false : ASD
false : 5374
false : 8288 SELKIRK
false : 1558 NICHOLS
true : 1400 AMERICAN LN
false : None of the above
我有以下 xml 数据。 <question>
中有问题,<answer>
中有所有可能的答案。 <answer>
标签具有 "correct" 属性,这是该问题的正确答案。
所以在这里我试图读取 <answer>
的这个 "Correct" 属性。
在这里,当我使用 "simplexml_load_string" 函数时,它将 xml 转换为 php 数组,但它不会 return 这个 "Correct" 属性。
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<questions>
<question type="1" text="Which one of the following addresses is associated with you?">
<answer correct="false">ABC</answer>
<answer correct="false">PQR</answer>
<answer correct="false">ASD</answer>
<answer correct="false">5374 </answer>
<answer correct="false">8288 SELKIRK</answer>
<answer correct="false">1558 NICHOLS</answer>
<answer correct="true">1400 AMERICAN LN</answer>
<answer correct="false">None of the above</answer>
</Question>
</Questions>
我怎样才能做到这一点?
Xml 区分大小写。如果将 </Question>
和 </Questions>
变成小写,一切正常:
$xml = simplexml_load_string($str);
foreach($xml->xpath('/questions/question/answer') as $ans)
echo $ans['correct'] .' : ' . $ans . "\n";
结果:
false : ABC
false : PQR
false : ASD
false : 5374
false : 8288 SELKIRK
false : 1558 NICHOLS
true : 1400 AMERICAN LN
false : None of the above