This blog has been neglected due to the success of Solidshops.com
17Feb/08
capture the encoding from the main XML file tag
To capture the encoding from the XML tag of the file. We need to read the first line, and pass it to the function to capture the encoding value.
The first line looks like this:
< ?xml version="1.0" encoding="utf-8"? >
The PHP code:
PHP:
function readfirstline($file){ return $firstline; } function get_attr( $line ,$attr) { return $arr_enc [1]; } $path = "home/user/feeds/id.xml"; $line= readfirstline($path);
Popularity: 4% [?]
10Aug/07
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% [?]


