使用php从另一个域解析xml

时间:2012-02-20 20:15:50

标签: php xml cross-domain

我想解析位于另一个域的xml文档。 我想用php做这个。 有可能吗?

我已经尝试过simpleXML但似乎没有用。

1 个答案:

答案 0 :(得分:3)

首先:http://www.stevymahy.com/xml/test.xml格式错误的XML。在修复此文档之前,您将无法对其进行解析。

现在是什么

    <?xml version="1.0" encoding="utf-8"?>
    <mahy>
    <!-- Diaporama -->
        <diaporama duree="4">
        <photo fichier="/admin/uploads/stevy01_001.jpg"></photo>
        <photo fichier="/admin/uploads/stevy05_001.jpg"></photo>
        <photo fichier="/admin/uploads/stevy06_001.jpg"></photo>
        <photo fichier="/admin/uploads/DSC_8133-Modifier.jpg"></photo>
        <photo fichier="/admin/uploads/appuyevelo.jpg"></photo>
        <photo fichier="/admin/uploads/atriumplus.jpg"></photo>
        <photo fichier="/admin/uploads/balancoirine.jpg"></photo>
    </diaporama>

    <!-- Biographie -->
    <bio textetaille="16" ><![CDATA[jhkjdhgkjdhgjksdghkjshgjkshdgjkhsdgjksdhgjksdhgkj]]></bio>

它只是落后......编辑:我发布了完整的XML。它不会落后(不再?)。然而,diaporama和bio元素必须包含在共同的根节点中。所以也许在<root>

中包装所有内容

修复后

http://www.php.net/manual/en/function.file-get-contents.php http://www.php.net/manual/en/simplexml.examples-basic.php

    // Get the contents of the XML
    $str_xml = file_get_contents('http://www.stevymahy.com/xml/test.xml');

    // Parse the XML
    $obj_xml = new SimpleXMLElement($str_xml);

    // ...do something with the parsed XML object