如何使用 PHP 提取以下 xml
How to extract following xml using PHP
<?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;
}
<?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;
}