<?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
之间的-
,那么我有很多类别,比如人的详细信息,我如何为它创建循环来检索?
若要更正对已解析的 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;
}