我有一個XML字符串,我試圖提取兒童標籤的名稱。每個兒童標籤都是自動關閉的。我試圖用SimpleXMLElement
XML:PHP只解析一系列的兒童中的第一個
$xml_str = '1<?xml version="1.0" encoding="UTF-8"?><parent><personal_data><child1 attr="sth /><child2 attr=sth2/></personal_data><personal_data><child1 attr="sth /><child2 attr=sth2/></personal_data</parent>';
$sxe = new SimpleXMLElement($xml);
//get the children from the parent
$sxe = $sxe->children();
echo $sxe;
$form_mappers = array();
foreach ($sxe->children() as $child){
array_push($form_mappers, $child->getName());
}
echo var_dump($form_mappers); //only children from the first parent
這僅獲得第aParent
節點的孩子。爲什麼我不能得到第二個孩子的節點?
@Ghost我認爲,任何畸形的產物我把一個樣本放在一起。這個驗證器http://xmlgrid.net/validator.html說我的實際XML是有效的。 – 1252748 2015-02-12 00:10:04
無論如何預期輸出是什麼? 4個孩子?你需要循環內eeach個人資料 – Ghost 2015-02-12 00:11:04
是啊,四個孩子'child1,child2,child1,child1' – 1252748 2015-02-12 00:12:37