1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Reading XML File in PHP

Discussion in 'PHP' started by kathi, Mar 7, 2007.

  1. #1
    Hi Guys,

    I want to know how we will read the comment line from XML in PHP?
     
    kathi, Mar 7, 2007 IP
  2. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #2
    Use php's DOM and XML functions. It's a bit easier with php5.
    I don't have a tutorial link handy, but you can take a look at w3schools.com
    Bye :)
     
    JEET, Mar 7, 2007 IP
  3. PhatDV

    PhatDV Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If you want something that doesn't require php5, or specific modules loaded into php, then look for an XML parser class. The advantage of this is that your code will work on pretty much any php enabled web server.

    I personally use Last RSS http://lastrss.oslab.net/ which I find works well, but others will have their own preferences.
     
    PhatDV, Mar 7, 2007 IP
  4. oziman

    oziman Active Member

    Messages:
    199
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #4
    MagpieRSS is a great, easy to use tool that works with PHP 4 and 5.
     
    oziman, Mar 8, 2007 IP
  5. jitesh

    jitesh Peon

    Messages:
    81
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    <?php
    LIST OF THE XML FILES
    $arfiles= array("basic2.xml","dataset.xml");

    foreach($arfiles as $arFile)
    {
    echo "<BR>-------------------------------------------------<BR>";
    echo "<BR>Parsing ".$arFile."<BR>";
    $insXmlParser= new clsXmlParser($arFile);
    if($aArray=$insXmlParser->Parse()) {
    echo "<pre>";
    print_r($aArray);
    //echo LIST_CONTENTS($aArray);
    }
    echo "<BR>-------------------------------------------------<BR>";
    }
    // Simple XML Parser

    class clsXmlParser {

    // general vars
    var $sTitle = "";
    var $sLink = "";
    var $sDescription = "";
    var $arItems = array();
    var $arsub = array();
    var $itemCount = 0;
    var $prvTag="";
    var $uFiles = '';
    var $xml_parser;
    var $curTag="";

    function clsXmlParser($uFiles)
    {

    $this->uFiles = $uFiles;

    }

    function startElement($parser, $name, $attrs) {

    $this->curTag .= "^$name";
    //echo "start: ".$this->curTag." <BR>";

    }

    function endElement($parser, $name)
    {

    $caret_pos = strrpos($this->curTag,'^');
    $this->curTag = substr($this->curTag,0,$caret_pos);
    //echo "end: ".$this->curTag." <BR>";
    }

    function characterData($parser, $data) {

    if(trim($data) != "")
    {
    if(trim($this->prvTag) == "")
    $this->prvTag=$this->curTag;
    elseif(trim($this->prvTag) == trim($this->curTag))
    {
    $this->arItems[] = $this->arsub;
    $this->arsub = array();

    }

    //find current element
    $c_pos = strrpos($this->curTag,'^');
    $c_len = strlen($this->curTag);
    $c_val = substr($this->curTag,($c_pos+1),$c_len);

    //set data to sub array with the element name as the key
    $this->arsub[$c_val] = $data;


    }

    }



    function Parse()
    {
    $this->xml_parser = xml_parser_create();


    xml_set_object($this->xml_parser, &$this);

    xml_set_element_handler($this->xml_parser, "startElement", "endElement");
    xml_set_character_data_handler($this->xml_parser, "characterData");

    if (!($fp = fopen($this->uFiles,"r")))
    {
    die ("could not open XML for input");
    }

    while ($data = fread($fp, 4096))
    {
    if (!xml_parse($this->xml_parser, $data, feof($fp)))
    {
    die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->xml_parser)), xml_get_current_line_number($this->xml_parser)));
    }
    }
    xml_parser_free($this->xml_parser);

    //to handle the last array element
    if(count($this->arsub)>0)
    {
    $this->arItems[] = $this->arsub;
    $this->arsub = array();
    }

    return $this->arItems;

    }

    }

    ///----------END OF CLASS


    function LIST_CONTENTS($arrayname,$tab="&nbsp&nbsp&nbsp&nbsp",$indent=0)
    {
    // recursively displays contents of the array and sub-arrays:
    // This function (c) Peter Kionga-Kamau
    // Free for unrestricted use, except sale - do not resell.
    // use: echo LIST_CONTENTS(array $arrayname, string $tab, int $indent);
    // $tab = string to use as a tab, $indent = number of tabs to indent result
    $retval=$currenttab="";
    while(list($key, $value) = each($arrayname))
    {
    for($i=0; $i<$indent; $i++) $currenttab .= $tab;
    if (is_array($value))
    {
    $retval .= "$currenttab$key : Array: <BR>$currenttab{<BR>";
    $retval .= LIST_CONTENTS($value,$tab,$indent+1)."$currenttab}<BR>";
    }
    else $retval .= "$currenttab$key => $value<BR>";
    $currenttab = NULL;
    }
    return $retval;
    }


    ?>
     
    jitesh, Mar 14, 2007 IP
  6. kathi

    kathi Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks every body.
     
    kathi, Mar 14, 2007 IP