<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person>
<id>rfBRFK4d1s</id>
<first-name>Thamaraiselvam</first-name>
<last-name>T</last-name>
<headline>Software Development Intern at Snowman Branding Services Pvt. Ltd</headline>
<picture-url>https://media.licdn.com/mpr/mprx/0_C-W9UUXaoLCdgSJ2CvJLUJ-mokL4xHd28KgWU4GxzTAd8uOuaqMVzZnlXo56pfHh_AwHNxp2yCIc</picture-url>
<industry>Computer Software</industry>
<educations total="2">
<education>
<school-name>Knowledge Institute of Technology</school-name>
<field-of-study>Computer Science and Engineering</field-of-study>
<start-date>
<year>2011</year>
</start-date>
<end-date>
<year>2015</year>
</end-date>
<degree>B.E</degree>
</education>
<education>
<school-name>Knowledge institute of technology</school-name>
<field-of-study>Computer Engineering</field-of-study>
<start-date>
<year>2011</year>
</start-date>
<end-date>
<year>2015</year>
</end-date>
<degree>Bachelor of Engineering (BE)</degree>
</education>
</educations>
</person>
我尝试使用PHP提取上面的xml,并尝试使用以下代码
$xml_response="my xml contents";
$xml = simplexml_load_string($xml_response);
echo $xml->person[0]['headers'] . "<br>";
echo $xml->id;
我可以获得id
但我无法获得其他内容和
echo $xml->first-name;
它显示错误,因为-
和first
之间的name
然后我有这么多类别的人详细信息如何创建循环以便检索?
答案 0 :(得分:1)
要纠正已解析的XML的遍历,您必须将其视为对象。举几个例子:
$xml = simplexml_load_string($xml);
echo $xml->id . PHP_EOL;
echo $xml->educations->education[0]->{'school-name'} . PHP_EOL;
echo $xml->educations->education[0]->{'end-date'}->year . PHP_EOL;
注意:强>
标<something-with-something>
必须由{'something-with-something'}
表达式正确处理。
如果你想要迭代节点(这里是education
):
foreach($xml->educations->education as $item) {
echo $item->{'school-name'} . PHP_EOL;
echo $item->degree . PHP_EOL;
}