Need some inputs parsing xml using SAX parser and PHP


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Need some inputs parsing xml using SAX parser and PHP

  1. #1
    Join Date
    May 2009
    Posts
    1

    Unhappy Need some inputs parsing xml using SAX parser and PHP

    Hi All,

    I have to parse my large xml file into small chunks. I thought of going ahead with SAX parser than DOM. Does anyone having idea how to parse the xml file using SAX.

    Here is the format of my xml file:

    <companies>

    <company>
    <product>
    <element ></element>
    <element ></element>
    </product>
    <product>
    <element ></element>
    <element ></element>
    </product>
    </company>

    <company>
    <product>
    <element ></element>
    <element ></element>
    </product>
    <product>
    <element ></element>
    <element ></element>
    </product>
    </company>

    <company>
    <product>
    <element></element>
    <element ></element>
    </product>
    <product>
    <element ></element>
    <element ></element>
    </product>
    </company>

    </companies>

    I have to split out each company details and save them as individual XML file.

    I am PHP developer. So any idea using sax parser to split each company details with the help of PHP would be appreciated.


    Regards,
    Krish

  2. #2
    Join Date
    May 2009
    Posts
    60
    Code:
    <?php
    /*
    	PHP code to read and process the links.xml file
    	Currently displays each link as an unordered list
    */
    
    $g_elem = "";
    $cn=0;
    $dateiname="company";
    $filename="";
    $datei;
    
    
    function startElemHandler($parser, $name, $attribs) {
    	if (strcasecmp($name, "company") == 0) {
            global $g_elem;
            global $dateiname;
            global $datei;
            global $cn;
            global $file;
            $cn= $cn + 1;
            $filename=$dateiname.$cn.".xml";
            $datei = fopen($filename, "w");
    
            $g_elem = $name;
    		echo "<company>\n";
            fwrite($datei, "<company>\n");
    
    	}
        if (strcasecmp($name, "name") == 0) {
            global $g_elem;
            global $datei;
            $g_elem = $name;
    		echo "<name>\n";
            fwrite($datei, "<name>\n");
    	}
    	if (strcasecmp($name, "product") == 0) {
            global $g_elem;
            global $datei;
            $g_elem = $name;
    		echo "<product>\n";
            fwrite($datei, "<product>\n");
    	}
    	if (strcasecmp($name, "element") == 0) {
            global $g_elem;
            global $datei;
    		$g_elem = $name;
    		echo "<element>";
            fwrite($datei, "<element>");
    	}
    }
    function textData( $parser, $text )
      {
          global $g_elem;
          global $datei;
          if($g_elem == "element")
        {
           echo "$text";
           fwrite($datei, $text);
        }
         if($g_elem == "name")
        {
           echo "$text";
           fwrite($datei, $text);
        }
      }
    function endElemHandler($parser, $name) {
    	if (strcasecmp($name, "company") == 0) {
            global $datei;
    		echo "</company>";
            fwrite($datei, "</company>\n");
            //fclose($datei);
    	}
        if (strcasecmp($name, "name") == 0) {
            global $datei;
    		echo "</name>";
            fwrite($datei, "</name>");
    	}
        if (strcasecmp($name, "product") == 0) {
            global $datei;
    		echo "</product>";
            fwrite($datei, "</product>");
    	}
    	if (strcasecmp($name, "element") == 0) {
            global $datei;
    		echo "</element>";
            fwrite($datei, "</element>");
    	}
    }
    
    
    /* create the parser */
    $parser = xml_parser_create();
    xml_set_element_handler($parser, startElemHandler, endElemHandler);
    xml_set_character_data_handler( $parser, textData );
    
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    
    // read the contents of the links file
    $strXML = implode("",file('company.xml'));
    
    // output each link 
    xml_parse($parser, $strXML);
    
    // clean up - we're done
    xml_parser_free($parser);
    ?>

    Helmut Hagemann

Similar Threads

  1. How obtain the three phones in this xml?
    By Corretja in forum XML
    Replies: 0
    Last Post: 07-09-2006, 01:43 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center