【问题标题】:php xml parsing with uri [duplicate]使用uri解析php xml [重复]
【发布时间】:2014-05-30 12:43:14
【问题描述】:

如何解析下面的xml代码?我正在尝试以下操作: echo $xml[0]->entry[0]->content[0];但我得到空值

<entry>
    <id>https://api.datamarket.azure.com/Data.ashx/MRC/MicrosoftAcademic/v2/Author(101612)</id>
    <category term="MRC.MicrosoftAcademic.Author" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <link rel="edit" title="Author" href="Author(101612)" /><title />
    <updated>2014-05-26T12:45:01Z</updated>
    <author>
    <name />
    </author>
    <content type="application/xml">
    <m:properties>
    <d:ID m:type="Edm.Int32">101612</d:ID>
    <d:Name>Omar Abou Khaled</d:Name>
    <d:NativeName m:null="true" />
    <d:Affiliation>University of Applied Sciences of Western Switzerland</d:Affiliation>
    <d:AffiliationID m:type="Edm.Int32">0</d:AffiliationID>
    <d:Email m:null="true" />
    <d:Homepage>http://omar.aboukhaled.home.hefr.ch/</d:Homepage>
    <d:Version m:type="Edm.Int32">11024</d:Version>
    <d:LinkedInUrl m:null="true" />
    <d:WikipediaUrl m:null="true" />
    <d:TwitterUserName m:null="true" />
    <d:ResearchInterests m:null="true" />
    </m:properties>
    </content>
</entry>

【问题讨论】:

    标签: php xml-parsing


    【解决方案1】:

    如果您使用 simplexml 解析此 xml

    $xml = simplexml_load_file(__DIR__ . '/file.xml');
    

    enty 是根节点。所以要获得 content 节点 - 应该使用直接调用内容:

    print_r($xml->content);
    

    获取内容类型:

    print_r($xml->content['type']);
    

    等等。

    Jay 在评论中留下的链接非常有用。

    还有关于命名空间的错误,要快速消除它,只需更新根节点,如下所示:

    <entry xmlns:m="/" xmlns:d="/">
    

    【讨论】:

    • 它显示此错误:尝试在第 5 行的 C:\wamp\www\mas\test.php 中获取非对象的属性
    • 能否请您分享 test.php 文件中的代码(我猜问题可能是 xml 文件的路径不正确)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-10
    • 2012-05-05
    • 1970-01-01
    相关资源
    最近更新 更多