SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    parsing xml with php, children

    Hello I successfully created my parser based on this article http://articles.sitepoint.com/articl...arsing-rss-1-0

    Everything is working great except one thing since my xml is formated a little different and I am totally lost on how to assign variable to the children of <photos>.

    xml portion

    Code:
    <item>
      <url /> 
      <name /> 
    - <photos>
      <photo>1020944_0.jpg</photo> 
      <photo>1020944_1.jpg</photo> 
      <photo>1020944_2.jpg</photo> 
      </photos>
      <user_id /> 
     </item>
    PHP code

    Code:
    <?
    global $insideitem, $tag, $name, $photos, $user_id;
    
    global $count,$db;
    
    $db = mysql_connect("localhost", "user","pass");
    mysql_select_db("db_name",$db);
    $result = mysql_query("SELECT user_id FROM table,$db);
    while ($myrow = mysql_fetch_array($result)){
    $uid=$myrow['user_id'];
    $UN_ID[$uid]=$uid;
    
    }
    
    
    
    $count=1;
    $count2=1;
    
    
    // ##########################################################
    // ************* START ELEMENT FUNCTION *********************
    // ##########################################################
    
    
    function startElement($parser, $name, $attrs) {
    
    global $insideitem, $tag, $name, $photos, $user_id;
    
    if ($insideitem) {
    $tag = $name;
    } 
    
    elseif($name == "ITEM"){
    $insideitem = true;
    }
    
    }
    
    
    function endElement($parser, $name) {
    
    global $insideitem, $tag, $name, $photos, $user_id;
    
    global $count,$count2,$db,$UN_ID;
    
    if ($name == "ITEM") {
    
    
    if(!$UN_ID[$unique_id]){
    
    
    $name=addslashes($name);
    $photo1=addslashes($photo);
    $photo2=addslashes($photo);
    $photo3=addslashes($photo);
    $photo4=addslashes($photo);
    $user_id=addslashes($category);
    
    
    $sql = "INSERT INTO table 
    (
    name,
    photo1,
    photo2,
    photo3,
    photo4,
    user_id
    )
     
    VALUES 
    
    (
    '$name',
    '$photo',
    '$photo',
    '$photo',
    '$photo',
    '$user_id',
    )";
    
    
    
    $resultupdate = mysql_query($sql);
    }
    $name='';
    $photos='';
    $user_id='';
    
    	
    }
    }
    
    
    
    function characterData($parser, $data) {
    
    global $insideitem, $tag, $name, $photos, $user_id;
    
    
    if ($insideitem) {
    
    switch ($tag) {
    
    case "NAME":
    $name .= $data;
    break;
    
    case "PHOTOS":
    $photos .= $data;
    break;
    
    case "USER_ID":
    $user_id .= $data;
    break;
    
    }
    
    }
    
    }
    
    
    $xml_parser = xml_parser_create(); 
    
    
    xml_set_element_handler($xml_parser, "startElement", "endElement"); 
    
    xml_set_character_data_handler($xml_parser, "characterData"); 
    
    $fp = fopen("../myfile.xml","r") 
           or die("Error reading RSS data."); 
    
    
    while ($data = fread($fp, 4096)) 
    
    // Parse each 4KB chunk with the XML parser created above 
    xml_parse($xml_parser, $data, feof($fp)) 
    
    // Handle errors in parsing 
    or die(sprintf("XML error: %s at line %d",  
    xml_error_string(xml_get_error_code($xml_parser)),  
    xml_get_current_line_number($xml_parser))); 
    
    
    fclose($fp); 
    
    
    // ##########################################################
    // *********************** FREE MEMORY **********************
    // ##########################################################
    
    xml_parser_free($xml_parser);
    ?>
    The number of <photo> tags can range between 1-4. I have tried searching everywhere for info on how to do this and tried everything but I just cant get it. After several days of this giving me headaches I really hope some one can enlighten me.

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Fread!

    Welcome to Sitepoint Forums!

    It would be really easier to parse any types of XML to any depth nodes if you use SIMPLEXML.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


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
  •