Got a lil problem with XML Parser

Discussion in 'PHP' started by The Webmaster, Sep 1, 2006.

  1. #1
    I am coding an XML Parser.
    The XML code is something like this -
    <KEY>
    <TDS>
    <U>http://example.com/value.php?key=26893</U>
    <U>http://example.com/value.php?key=27736</U>
    <U>http://example.com/value.php?key=35075</U>
    <U>http://example.com/value.php?key=32305&amp;page=2</U>
    <U>http://example.com/value.php?key=85890</U>
    <U>http://example.com/value.php?key=68523&amp;page=2</U>
    <U>http://example.com/value.php?key=87993&amp;page=2</U>
    <U>http://example.com/value.php?key=91394</U>
    <U>http://example.com/value.php?key=78336</U>
    <U>http://example.com/value.php?key=37557</U>
    <U>http://example.com/value.php?key=91760</U>
    <U>http://example.com/value.php?key=63695&amp;page=2</U>
    <U>http://example.com/value.php?key=86436</U>
    <U>http://example.com/value.php?key=91130</U>
    <U>http://example.com/value.php?key=26130</U>
    <U>http://example.com/value.php?key=1810</U>
    <U>http://example.com/value.php?key=29303</U>
    <U>http://example.com/value.php?key=80196</U>
    </TDS>
    </KEY>
    HTML:
    And the Code for parser is -

    
    <?php
    
    $xml_file = "search.xml";
    
    $xml_link_key  = "*KEY*TDS*U";
    $link_array = array();
    
    $counter = 0;
    function startTag($parser, $data){
        global $current_tag;
        $current_tag .= "*$data";
    }
    
    function endTag($parser, $data){
        global $current_tag;
        $tag_key = strrpos($current_tag, '*');
        $current_tag = substr($current_tag, 0, $tag_key);
    }
    
    function contents($parser, $data)
    {
        global $current_tag, $xml_link_key, $xml_title_key, $counter, $link_array;
        if($current_tag==$xml_link_key)
    	{
                $link_array[$counter]->link = $data;
    			$counter++;
        }
    }
    
    $xml_parser = xml_parser_create();
    
    xml_set_element_handler($xml_parser, "startTag", "endTag");
    
    xml_set_character_data_handler($xml_parser, "contents");
    
    $fp = fopen($xml_file, "r") or die("Could not open file");
    
    $data = fread($fp, 52208) or die("Could not read file");
    
    if(!(xml_parse($xml_parser, $data, feof($fp)))){
        die("Error on line " . xml_get_current_line_number($xml_parser));
    }
    
    xml_parser_free($xml_parser);
    
    fclose($fp);
    
    ?>
    
    Code (markup):
    The Output is like this -
    http://www.rankalert.net/xml/test.php

    Now as you can see
    It is working fine with URLs like
    http://example.com/value.php?key=26893
    But when it encounters a URL like
    http://example.com/value.php?key=32305&amp;page=2
    It takes "http://example.com/value.php?key=32305" as one URL and "&" as 2nd URL and "Page=2" as 3rd URL

    Can anyone help me with how to resolve this issue??
     
    The Webmaster, Sep 1, 2006 IP
  2. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #2
    According to documentation:

    I am modified your code, now it works. Empty object converted to array (why are you using an object?), removed counter (I think you don't need it), some other changes. See the code:

    
    <?php
    
    $xml_file = "search.xml";
    
    $xml_link_key  = "*KEY*TDS*U";
    $link_array = array();
    
    $cont = '';
    
    function startTag($parser, $data)
    {
        global $current_tag, $cont;
        $current_tag .= "*$data";
        $cont='';
    }
    
    function endTag($parser, $data){
        global $current_tag, $link_array;
        global $cont;
        $tag_key = strrpos($current_tag, '*');
        $current_tag = substr($current_tag, 0, $tag_key);
        $link_array[]['link'] = $cont;
    }
    
    function contents($parser, $data)
    {
        global $current_tag, $xml_link_key, $xml_title_key, $link_array, $cont;
        if($current_tag==$xml_link_key)
          $cont .= $data;
    }
    
    $xml_parser = xml_parser_create();
    
    xml_set_element_handler($xml_parser, "startTag", "endTag");
    
    xml_set_character_data_handler($xml_parser, "contents");
    
    $fp = fopen($xml_file, "r") or die("Could not open file");
    
    $data = fread($fp, 52208) or die("Could not read file");
    
    if(!(xml_parse($xml_parser, $data, feof($fp)))){
        die("Error on line " . xml_get_current_line_number($xml_parser));
    }
    
    xml_parser_free($xml_parser);
    
    fclose($fp);
    
    ?>
    
    PHP:
     
    wmtips, Sep 1, 2006 IP
    The Webmaster likes this.
  3. The Webmaster

    The Webmaster IdeasOfOne

    Messages:
    9,516
    Likes Received:
    718
    Best Answers:
    0
    Trophy Points:
    360
    #3
    Thanks a lot buddy...
    I didnt think that way.....

     
    The Webmaster, Sep 1, 2006 IP