在PHP中解析复杂的多级XML

时间:2017-04-15 20:56:42

标签: php xml

我试图在PHP中解析复杂的多级XML。 XML的一部分如下所示:

<records>
    <record>
        <title language="eng">Barcode Technology Acceptance and Utilization in Health Information Management Department at Academic Hospitals According to Technology Acceptance Model</title>
        <authors>
            <author>
                <name>Asghar Ehteshami</name>
                <email>-</email>
                <affiliationId>1</affiliationId>
            </author>
        </authors>
        <keywords language="eng">
            <keyword>Barcode</keyword>
            <keyword>Technology Acceptance Model</keyword>
            <keyword>Health Information Technology</keyword>
        </keywords>
    </record>
    <record>
        <title language="eng">Cardiac Structural and Functional Changes Evaluated by Transthoracic and Tissue Doppler Echocardiography in Adult Patients with Sickle Cell Disease</title>
        <authors>
            <author>
                <name>Mojdeh Dabirian</name>
                <email>-</email>
                <affiliationId>1</affiliationId>
            </author>
            <author>
                <name>Ghasem Janbabaei</name>
                <email>-</email>
                <affiliationId>2</affiliationId>
            </author>
            <author>
                <name>Hossein Karami</name>
                <email>-</email>
                <affiliationId>3</affiliationId>
            </author>
            <author>
                <name>Maryam Nabati</name>
                <email>-</email>
                <affiliationId>4</affiliationId>
            </author>
            <author>
                <name>Mohsen Aarabi</name>
                <email>-</email>
                <affiliationId>5</affiliationId>
            </author>
            <author>
                <name>Morteza Namazi</name>
                <email>-</email>
                <affiliationId>6</affiliationId>
            </author>
            <author>
                <name>Hadi Darvishi-Khezri</name>
                <email>-</email>
                <affiliationId>7</affiliationId>
            </author>
        </authors>
        <keywords language="eng">
            <keyword>Ventricular structure</keyword>
            <keyword>Ventricular function</keyword>
            <keyword>Transthoracic echocardiography</keyword>
            <keyword>Sickle cell disease</keyword>
            <keyword>Tissue Doppler echocardiography</keyword>
        </keywords>
    </record>
    <record>            
    ...

我想让它解析如下:

标题1 作者姓名1,作者姓名2 ...

标题2
作者姓名1,作者姓名2 ... ...

无论如何,即使在具有多个作者姓名的记录中,我也始终只得到作者的名字,例如XML示例中的记录号2。

谢谢!

添加我的代码:

$xml=simplexml_load_file("feed_doaj-v25-i1.000.xml") or die("Error: Cannot create object");
foreach($xml as $article) {
  $title = $article->title;
  $name = $article->authors->author->name;


  echo '<h4>'.$title.'</h4>';
  echo  '<p>'.$name.'</p><br/>';

  ?>

在我的示例中,我从XML获得了以下内容:

条形码技术在学术医院卫生信息管理部门的接受和利用根据技术验收模型
Asghar Ehteshami

成人镰状细胞病患者的经胸和组织多普勒超声心动图评估心脏结构和功能变化
Mojdeh Dabirian

所以,问题是,在第二个标题中,我的代码只从XML中提取名字作者姓名,即使该文章有7个名称。

希望我清楚一点我的问题:)

1 个答案:

答案 0 :(得分:0)

如果你想获得所有作者,你必须遍历它们。只需再做一个简单的循环来解析作者:

foreach( $xml as $article ) {
  $title = $article->title;
  echo '<h4>'.$title.'</h4>';
  foreach( $article->authors->author as $author ) {
      echo  '<p>'.$author->name.'</p>';
  }
  echo '<br/>';
}

对于此代码,输出将为:

Barcode Technology Acceptance and Utilization in Health Information Management Department at Academic Hospitals According to Technology Acceptance Model

Asghar Ehteshami


Cardiac Structural and Functional Changes Evaluated by Transthoracic and Tissue Doppler Echocardiography in Adult Patients with Sickle Cell Disease

Mojdeh Dabirian

Ghasem Janbabaei

Hossein Karami

Maryam Nabati

Mohsen Aarabi

Morteza Namazi

Hadi Darvishi-Khezri