按属性值获取xml数据

时间:2013-11-11 21:13:23

标签: php xml

我有这个XML:

<eSummaryResult>
    <DocSum>
        <Id>11482001</Id>
        <Item Name="PubDate" Type="Date">2001 Jun</Item>
        <Item Name="EPubDate" Type="Date" />
        <Item Name="Source" Type="String">Adverse Drug React Toxicol Rev</Item>
        <Item Name="AuthorList" Type="List">
            <Item Name="Author" Type="String">Mantle D</Item>
            <Item Name="Author" Type="String">Gok MA</Item>
            <Item Name="Author" Type="String">Lennard TW</Item>
        </Item>
        <Item Name="LastAuthor" Type="String">Lennard TW</Item>
        <Item Name="Title" Type="String">Adverse and beneficial effects of plant extracts on skin and skin disorders.</Item>
        <Item Name="Volume" Type="String">20</Item>
        <Item Name="Issue" Type="String">2</Item>
        <Item Name="Pages" Type="String">89-103</Item>
        <Item Name="LangList" Type="List">
            <Item Name="Lang" Type="String">English</Item>
        </Item>
        <Item Name="NlmUniqueID" Type="String">9109474</Item>
        <Item Name="ISSN" Type="String">0964-198X</Item>
        <Item Name="ESSN" Type="String" />
        <Item Name="PubTypeList" Type="List">
            <Item Name="PubType" Type="String">Journal Article</Item>
            <Item Name="PubType" Type="String">Review</Item>
        </Item>
        <Item Name="RecordStatus" Type="String">PubMed - indexed for MEDLINE</Item>
        <Item Name="PubStatus" Type="String">ppublish</Item>
        <Item Name="ArticleIds" Type="List">
            <Item Name="pubmed" Type="String">11482001</Item>
            <Item Name="eid" Type="String">11482001</Item>
            <Item Name="rid" Type="String">11482001</Item>
        </Item>
        <Item Name="History" Type="List">
            <Item Name="pubmed" Type="Date">2001/08/03 10:00</Item>
            <Item Name="medline" Type="Date">2002/01/23 10:01</Item>
            <Item Name="entrez" Type="Date">2001/08/03 10:00</Item>
        </Item>
        <Item Name="References" Type="List" />
        <Item Name="HasAbstract" Type="Integer">1</Item>
        <Item Name="PmcRefCount" Type="Integer">3</Item>
        <Item Name="FullJournalName" Type="String">Adverse drug reactions and toxicological reviews</Item>
        <Item Name="ELocationID" Type="String" />
        <Item Name="SO" Type="String">2001 Jun;20(2):89-103</Item>
    </DocSum>
</eSummaryResult>

我想按名称获取一些项目,例如:

<Item Name="AuthorList" Type="List">
    <Item Name="Author" Type="String">Mantle D</Item>
    <Item Name="Author" Type="String">Gok MA</Item>
    <Item Name="Author" Type="String">Lennard TW</Item>
</Item>

在此代码中,如何获取Name =“Author”的项目?它必须打印Mantle D,Gok MA,......

我已经找到了如何使用attribute()获取属性值,但不是。

所有人!

1 个答案:

答案 0 :(得分:1)

simplexmlxpath将完成这项工作:

$xml = simplexml_load_string($x); // assume XML in $x
$authors = $xml->xpath("//Item[@Name='Author']");

// echo them out
foreach ($authors as $author) echo "$author <br />";

xpath-expression转换为:选择所有<Item> - 节点 - 无论它们在XML中的位置(双斜线) - 都设置了属性Name(@指属性)到'作者'。

看到它有效:http://codepad.viper-7.com/PQSDcV

相关问题