0
foreach($xml->property as $property ){
   foreach($property as $type){
$id = mysql_real_escape_string($property->id);
echo "ID: ".$id."<br/>";
$type = mysql_real_escape_string($type->uk);
echo "Type: ".$type."<br/>";

} }

我现在想回显 xml 文件的元素,id 可以回显,但是我无法得到它的类型,如果我使用上面的代码执行此操作,那么循环是 id 的两倍并且类型正在获取但是没有按我的意愿工作,所以我如何在没有 foreach 的情况下获得子元素 ..?

编辑:我也需要他们这样的东西,因为在回声之后,他们需要被添加到数据库中..

进出口

<propery>
<ID>R123412</ID>
<type>
<uk>Apartmen</uk>
</type>
</propery>
4

1 回答 1

0
$string = <<<XML
<?xml version='1.0'?> 
<propery>
<ID>R123412</ID>
<type>
<uk>Apartmen</uk>
</type>
</propery>
XML;

$xml = simplexml_load_string($string);

//print_r($xml);
foreach ($xml as $key => $value) {  
    if (is_object($value))
        foreach ($value as $k => $v) {
            echo "   ".$v;// because is object meens somthing under (the)
                   //you can also filter by keyname($key) 
        }
    echo "   ".$value; //this is the ID, you can also filter by keyname($key) 

}

结果是:R123412 公寓

于 2013-06-06T15:51:01.440 回答