Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从php解析XML_Php_Xml - Fatal编程技术网

从php解析XML

从php解析XML,php,xml,Php,Xml,我有这个xml文件(见下文),我想用php解析它以输出文件名和URL <?xml version="1.0" encoding="UTF-8"?> <OTA xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ota.xsd"> <Stable> <cheeseburger> <

我有这个xml文件(见下文),我想用php解析它以输出文件名和URL

<?xml version="1.0" encoding="UTF-8"?>
<OTA xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ota.xsd">
    <Stable>
        <cheeseburger>
            <Filename>crDroidAndroid-8.1-20180128-cheeseburger-v4.0-BETA5</Filename>
            <RomUrl
                id="rom"
                title="Downloads"
                description="Download latest builds">dl url here for cheeseburger</RomUrl>
        </cheeseburger>

        <dumpling>
            <Filename>crDroidAndroid-8.1-20180127-dumpling-v4.0-BETA</Filename>
            <RomUrl
                id="rom"
                title="Downloads"
                description="Download latest build">dl url here for dumpling</RomUrl>
        </dumpling>
     </Stable>
</OTA>
现在我被困在每个元素的循环中 这是我到目前为止的代码

<?php
// Devices.xml reader 

// check if file exists and read it
if (file_exists('devices.xml')) {
    $xml = simplexml_load_file('devices.xml');

    //echo $xml->Stable->dumpling->Filename;
    echo "<br>";
    foreach($xml->Stable as $device) {
        echo '<h2>' . $device->Filename . '</h2>';
    }
    echo "<br>";
    echo "<br>";
    //var_dump($xml);
} else {
    exit('Failed to open devices.xml.');
}
?>
Stable->dumpling->Filename;
回声“
”; foreach($xml->稳定为$device){ 回显“”。$device->Filename。“; } 回声“
”; 回声“
”; //var_dump($xml); }否则{ 退出('打开devices.xml失败'); } ?>
现在我确信可能有更好的选择,但我对php中的XML不太熟悉,因此非常感谢您的帮助


谢谢

$device
是一种您可以使用
foreach
循环的设备

然后,您可以访问
$key
以获取例如“cheeseburger”,以及
$item
(也是SimpleXMLElement类型)以获取诸如“Filename”之类的属性

<?php
// Devices.xml reader 

// check if file exists and read it
if (file_exists('devices.xml')) {
    $xml = simplexml_load_file('devices.xml');

    //echo $xml->Stable->dumpling->Filename;
    echo "<br>";
    foreach($xml->Stable as $device) {
        echo '<h2>' . $device->Filename . '</h2>';
    }
    echo "<br>";
    echo "<br>";
    //var_dump($xml);
} else {
    exit('Failed to open devices.xml.');
}
?>
if (file_exists('devices.xml')) {
    $xml = simplexml_load_file('devices.xml');
    foreach($xml->Stable as $device) {
        foreach ($device as $key => $item) {
            echo $key . "<br>";
            echo $item->Filename . "<br>";
            echo $item->RomUrl . "<br>";
            echo "<br>";
        }
    }
} else {
    exit('Failed to open devices.xml.');
}
cheeseburger
crDroidAndroid-8.1-20180128-cheeseburger-v4.0-BETA5
dl url here for cheeseburger

dumpling
crDroidAndroid-8.1-20180127-dumpling-v4.0-BETA
dl url here for dumpling