如何从解析的XML中获取价值

时间:2016-11-08 05:56:14

标签: php xml xml-parsing simplexml

这是我的名为test.xml的XML:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:test="http://www.test.com/rss/2.0">
<positionfeed>
    <test:import user="accounts@test.com" site="www.test.com" />
    <job>
        <id>83762455</id>
        <employer>Mount Ida College</employer>
        <title>Assistant Professor, Dental Hygiene</title>
        <description>As a faculty member, you will provide instruction in didactic courses, clinics, and/or labs in the Dental Hygiene Program.</description>
        <postingdate>2016-10-21</postingdate>
        <joburl>http://url.com</joburl>
        <location>Newton, MA</location>
        <category>Education</category>
    </job>
    <job>
        <id>83762405</id>
        <employer>Veterinary Orthopedic &amp; Sports Medicine Group</employer>
        <title>Overnight Recovery Technician</title>
        <description>As a faculty member, you will provide instruction in didactic courses, clinics, and/or labs in the Dental Hygiene Program.</description>
        <postingdate>2016-10-25</postingdate>
        <joburl>http://url.com</joburl>
        <location>Annapolis Junction, MD</location>
        <category>Installer/Technician</category>
    </job>
</positionfeed>
</rss>

这是我正在解析它的PHP页面:

<?php
    $languages = simplexml_load_file("test.xml");
    print_r($languages);

    foreach ($languages->job as $job) {
        echo $job->title;
        echo "<br>";
    }
?>

我想:

  

求职&GT; ID
  求职&GT;标题

等等

=============================

print_r($ languages)给出:

  

SimpleXMLElement Object([@attributes] =&gt; Array([version] =&gt; 2.0)   [positionfeed] =&gt; SimpleXMLElement对象([job] =&gt;数组([0] =&gt;   SimpleXMLElement对象([id] =&gt; 83762455 [雇主] =&gt; Mount Ida   大学[title] =&gt;牙科卫生助理教授[描述]   =&GT;作为教职员工,您将在牙科卫生计划中提供教学课程,诊所和/或实验室的指导。   [postingdate] =&gt; 2016-10-21 [joburl] =&gt; http://url.com [location] =&gt;   牛顿,马萨诸塞州[类别] =&gt;教育)[1] =&gt; SimpleXMLElement对象(   [id] =&gt; 83762405 [雇主] =&gt;兽医骨科&amp;运动药物   组[title] =&gt;隔夜恢复技术员[描述] =&gt;作为一个   教师,你将提供教学课程的教学,   牙科卫生计划的诊所和/或实验室。 [postingdate] =&gt;   2016-10-25 [joburl] =&gt; http://url.com [location] =&gt;安纳波利斯   Junction,MD [category] ​​=&gt;安装人员/技术人员))))

============================

我尝试了很多研究。

如果我手动删除以下三行,PHP代码工作正常并根据需要显示值,但我无法动态删除它们。

<rss version="2.0" xmlns:test="http://www.test.com/rss/2.0">
    <test:import user="accounts@test.com" site="www.test.com" />
</rss>

所以任何一种方式都可以工作,如果有人可以提供帮助,或者帮助我在PHP页面上加载时动态删除这三行,否则就能获得所需节点的值。

再次描述很长,我想:

  

求职&GT; ID
  求职&GT;标题

等等

0 个答案:

没有答案