使用XMLReader快速解析深层嵌套值

时间:2016-10-30 22:34:31

标签: php xml parsing xmlreader

我是XMLReader的新手,我想提取一些深层嵌套的节点。我无法抓住我想要的价值观。下面我有一个我正在解析的元素的例子,我正在使用的代码,以及我想要的值。链接到xml文件:http://datastore.unm.edu/schedules/current.xml

XML元素:

<unmschedule version="1.0.4" pubdate="2016-10-30-13:00:02" source="UNM">
<usage>
  <guideline url="http://xmlschedule.unm.edu/docs/xml-schedule-usage-guideline.html"/>
</usage>
<lookups>
  <parts-of-term>...</parts-of-term>
  <statuses>...</statuses>
</lookups>
<semester code="201680" name="Fall 2016" index="1">
  <campus code="ABQ" name="Albuquerque/Main">
    <college code="AP" name="School of Arch. and Planning">
      <department code="ABM" name="School Architecture Planning">
        <subject code="ARCH" name="Architecture">
          <course number="109" title="Design Fundamentals">
        <catalog-description>
         Studio/lecture.
        </catalog-description>
        <section crn="41646" number="002" part-of-term="1" status="S">
          <section-title/>
          <text>Cancel/Reschedule to CRN 57664 04/20/2016.</text>
          <instructional-method code=""/>
          <delivery-type code="LC">Lecture</delivery-type>
          <enrollment max="18">0</enrollment>
          <waitlist max="0">0</waitlist>
          <credits>3</credits>
        </section>
        <section crn="45817" number="008" part-of-term="1" status="A">
          <section-title/>
          <text/>
          <instructional-method code="ENH">Web Enhanced</instructional-method>
          <delivery-type code="LC">Lecture</delivery-type>

代码:

$map_url = 'http://datastore.unm.edu/schedules/current.xml'; 

$xml = new XMLReader;
$xml->open($map_url);

$xml->read();

while ($xml->read()) {
    if ($xml->nodeType == XMLReader::ELEMENT) {
            print $xml->semester->campus->college->department->subject->course.': ';
    } else if ($xml->nodeType == XMLReader::TEXT) {
            print $xml->semester->campus->college->department->subject->course->value.PHP_EOL;
    }
}

我想提取的节点:

1)在<course>标签中我想要“数字”和“标题”,例如109和设计基础。

2)在<section>标签中我想要“crn”和数字,例如41646和002

0 个答案:

没有答案