【问题标题】:cant get to the sub elements on xml file无法访问 xml 文件中的子元素
【发布时间】:2013-06-06 15:31:13
【问题描述】:
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 的情况下获得子元素..?

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

exmp

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

【问题讨论】:

  • &lt;type&gt;&lt;uk&gt; 中的值是否始终是您想要的?或者是&lt;type&gt;&lt;usa&gt;etc.?

标签: php xml file foreach echo


【解决方案1】:
$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 公寓

【讨论】:

  • 这就像一个魅力,但问题是我需要将值插入到数据库中,所以这不能解决我的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多