SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: PHP XML Parser

  1. #1
    SitePoint Zealot asrasrasr's Avatar
    Join Date
    Apr 2009
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP XML Parser

    Hello Friends,

    I am trying to prase a xml file.
    Code XML:
      <?xml version="1.0" encoding="utf-8" standalone="yes" ?> 
    - <dvds>
    - <dvd>
      <id>mfno1095257</id> 
      <amazonID>B00061RZY2</amazonID> 
    - <title>
    - <![CDATA[ Dad's Army/Virgin Soldiers/No Sex Please, We're British]]></title>
      <released>01 January 2020</released> 
      <category>Documentary</category> 
      <rrp>14.99</rrp> 
      <minimumPrice>11.19</minimumPrice> 
      <savingPrice>3.80</savingPrice> 
      <savingPercent>25.40</savingPercent> 
      </dvd>
    - <dvd>
      <id>mfno1095661</id> 
      <amazonID>B000LW7L36</amazonID> 
    - <title>
    - <![CDATA[ Ken Jacobs - New York Ghetto Fishmarket 1903  [1992] [US Import]]]> 
      </title>
      <released>01 January 2020</released> 
      <category>Documentary</category> 
      <minimumPrice>19.99</minimumPrice> 
      <savingPrice /> 
      <savingPercent /> 
      </dvd>
    - <dvd>
      <id>mfno109671</id> 
      <amazonID>B000LW7L36</amazonID> 
    - <title>
    - <![CDATA[ Ken Jacobs]]> 
      </title>
      <released>01 January 2020</released> 
      <category>Documentary</category> 
      <minimumPrice>19.99</minimumPrice> 
      </dvd>
      </dvds>


    This xml file i m parsing with following php code.
    Code PHP:
    <?php
                 // event handler for start elements in the XML category 1
     
                  function startElement($parser, $name, $attrs) 
    			      {
    				   global $lastTag;
    				  // get the tag name
    				   $lastTag = $name;
    			      }
     
    			    // event handler for end elements in the XML category 1
    			   function endElement($parser, $name) 
    			    {
    				// allow access to global variables
    				global $eachDvdLine,$dvdArray,$imageURL,$title,$dvdid,$dvdrrp,$savingPercent;
    				// determine the name of the tag in the XML category 1
     
    				   switch ($name) {
    					case "SAVINGPERCENT":
    						array_push($eachDvdLine,$title);
    						array_push($eachDvdLine,$dvdid);
    						array_push($eachDvdLine,$dvdrrp);
    						array_push($eachDvdLine,$savingPercent);
    						$title = '';
    						$dvdid = '';
    						$dvdrrp = '';
    						$savingPercent = '';
    						break;
                        case "DVD":
    						// add each price line to the whole array of prices, then reset the $eachDealLine
    						array_push($dvdArray,$eachDvdLine);
    						$eachDvdLine = array();
    						break; }			
    		     	}
     
    			// event handler for the XML character data (i.e. the actual data in the XML)
    			// event handler for the XML character data (i.e. the actual data in the XML)
    			function characterData($parser, $data) 
    			{
    				global $lastTag,$imageURL,$title,$dvdid,$dvdrrp,$savingPercent;
    				switch ($lastTag) {
    					case "TITLE":
    						$title .= $data;
    						break;
    					case "ID":
    						$dvdid = $data;
    						break;
    					case "RRP":
    						$dvdrrp = $data;
    						break;
    					case "SAVINGPERCENT":
    						$savingPercent = $data;
    						break;
               			}
    			   }

    In this code i m push array in case "SAVINGPERCENT": condition.

    But sum dvd does not have <savingPrice > tag or blank <savingPrice > tag.

    In that this unable to push array. Can anybody tell me that taht condition how i push arry on find minimumPrice case.

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Use SimpleXML or the DOM functions. A lot easier that way.

    http://us3.php.net/simplexml
    http://us3.php.net/manual/en/book.dom.php
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Member
    Join Date
    Jan 2006
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Simple XML makes life easy...

    OR just use a regex. I just wrote an example for both for getting Twitter status, given a twitter username....

    You can probs use this code...

    http://www.openzu.com/Code-Samples/P...r-Status/p-41/


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •