I have an XML string that I'm trying to extract the names of the children tags. Each child tag is self-closing. I'm trying to use SimpleXMLElement
$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
This only gets the children of the first aParent
node. Why can't I get the child nodes of the second?
If you're trying to get all <child*>
nodes, you need to iterate each <personal_data>
also to get inside the inner level:
$form_mappers = array();
foreach ($sxe->children() as $personal_data){
foreach($personal_data->children() as $child) {
$form_mappers[] = $child->getName();
}
}