10Aug/07
This blog has been neglected due to the success of Solidshops.com
writing the recursive path for all items that contain data in XML file
This little php functions shows how to write the recursive XMLpath for all nodes that contain values.
XML:
-
$xmlstr = <<<XML
-
<?xml version='1.0' standalone='yes'?>
-
<movies>
-
<movie>
-
<title>PHP: Behind the Parser</title>
-
<characters>
-
<character>
-
<name>Ms. Coder</name>
-
<actor>Onlivia Actora</actor>
-
</character>
-
<character>
-
<name>Mr. Coder</name>
-
<actor>El ActÓr</actor>
-
</character>
-
</characters>
-
<plot>
-
So, this language. It's like, a programming language. Or is it a
-
scripting language? All is revealed in this thrilling horror spoof
-
of a documentary.
-
</plot>
-
<rating type="thumbs">7</rating>
-
<rating type="stars">5</rating>
-
</movie>
-
</movies>
-
XML;
PHP:
-
$xml = new SimpleXMLElement($xmlstr);
-
//SimpleXML_recursive($xml->xpath('//movie/characters/character'));
-
SimpleXML_recursive($xml);function SimpleXML_recursive($xml,$parent="")
-
{
-
$child_count = 0;
-
foreach($xml as $key=>$value)
-
{
-
$child_count++;
-
if(SimpleXML_recursive($value,$parent."/".$key) == 0) // no childern
-
{
-
if($value->attributes() <> ""){
-
$att = "[@".$value->attributes()."]";
-
}
-
\n");
-
}
-
}
-
return $child_count;
-
}
This is the result:
/movie/title
/movie/characters/character/name
/movie/characters/character/actor
/movie/characters/character/name
/movie/characters/character/actor
/movie/plot
/movie/rating[@thumbs]
/movie/rating[@stars]
Now you can use this code to map the XML fields against the database fields.
Popularity: 2% [?]


