aRo` automating your e-commerce

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:
  1. function readfirstline($file){
  2.    $fp = @fopen($file, "r");
  3.    $firstline = fgets($fp);
  4.    fclose($fp);
  5.    return $firstline;
  6. } 
  7. function get_attr(  $line ,$attr) {
  8.   $start = strpos($line,"$attr");
  9.   $linesubstr($line,$start);
  10.   $arr_enc = split("\"",$line);
  11.  
  12.   return $arr_enc [1];
  13. }
  14.  
  15. $path = "home/user/feeds/id.xml";
  16. $line= readfirstline($path);
  17.        
  18. echo get_attr(  $line ,"encoding");

Popularity: 4% [?]

Filed under: php, xml No Comments
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:
  1. $xmlstr = <<<XML
  2. <?xml version='1.0' standalone='yes'?>
  3. <movies>
  4.  <movie>
  5.   <title>PHP: Behind the Parser</title>
  6.   <characters>
  7.    <character>
  8.     <name>Ms. Coder</name>
  9.     <actor>Onlivia Actora</actor>
  10.    </character>
  11.    <character>
  12.     <name>Mr. Coder</name>
  13.     <actor>El Act&#211;r</actor>
  14.    </character>
  15.   </characters>
  16.   <plot>
  17.    So, this language. It's like, a programming language. Or is it a
  18.    scripting language? All is revealed in this thrilling horror spoof
  19.    of a documentary.
  20.   </plot>
  21.   <rating type="thumbs">7</rating>
  22.   <rating type="stars">5</rating>
  23.  </movie>
  24. </movies>
  25. XML;

PHP:
  1. $xml = new SimpleXMLElement($xmlstr);
  2. //SimpleXML_recursive($xml-&gt;xpath('//movie/characters/character'));
  3. SimpleXML_recursive($xml);function SimpleXML_recursive($xml,$parent="")
  4. {
  5. $child_count = 0;
  6. foreach($xml as $key=&gt;$value)
  7. {
  8. $child_count++;
  9. if(SimpleXML_recursive($value,$parent."/".$key) == 0)  // no childern
  10. {
  11. if($value-&gt;attributes() &lt;&gt; ""){
  12. $att = "[@".$value-&gt;attributes()."]";
  13. }
  14. print($parent . "/" . (string)$key .$att. "
  15. \n");
  16. }
  17. }
  18. return $child_count;
  19. }

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% [?]

Filed under: php, xml No Comments