The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Results 1 to 5 of 5

Thread: xml to php parsing - how to get attribute info? (Not PHP 5)

  1. #1

    xml to php parsing - how to get attribute info? (Not PHP 5)

    Hi guys! I've found a really useful tutorial (http://www.kirupa.com/web/xml_php_pa...termediate.htm) on the kirupa site that has helped me out of a tight spot ... almost!

    I'm working on a server that is NOT php 5 and will not be for several weeks, so simpleXML is not a solution!a

    I'm parsing an XML file in order to build an sql insert statement (xml is the ONLY format I can get hold of the data, as supplied by client)


    in .xml file ....

    <Property>
    <Refnumber>13</Refnumber>
    <Postcode>DE223XK</Refnumber>
    <Rent Per="Month">450.00</Rent>


    I'm using the code from the tutorial mentioned above ...

    I can access the data from the 'refnumber' and 'postcode' but I don't know how to find out what the value is in the Per="" attribute :-|

    this is the last part of my code from the tutorial:

    echo "rentper:" . $property_array[$x]->rentper . "<br />\n";


    could anyone PLEASE help me ??? :-)

    thanks!!!

  2. #2
    Here use this for PHP4:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <Property>
            <Refnumber>13</Refnumber>
            <Postcode>DE223XK</Refnumber>
            <Rent Per="Month">450.00</Rent>
    </Property>
    PHP Code:
    class XMLParser{
        var 
    $parser;
        var 
    $filePath;
        var 
    $document;
        var 
    $currTag;
        var 
    $tagStack;
        function 
    XMLParser($path){
            
    $this->parser xml_parser_create();
            
    $this->filePath $path;
            
    $this->document = array();
            
    $this->currTag =& $this->document;
            
    $this->tagStack = array();
        }
        function 
    parse(){
            
    xml_set_object($this->parser$this);
            
    xml_set_character_data_handler($this->parser'dataHandler');
            
    xml_set_element_handler($this->parser'startHandler''endHandler');
            if(!(
    $fp fopen($this->filePath"r"))){
                die(
    "Cannot open XML data file: $this->filePath");
                return 
    false;
            }
            while(
    $data fread($fp4096)){
                if(!
    xml_parse($this->parser$datafeof($fp))){
                    die(
    sprintf("XML error: %s at line %d",
                    
    xml_error_string(xml_get_error_code($this->parser)),
                    
    xml_get_current_line_number($this->parser)));
                }
            }
            
    fclose($fp);
            
    xml_parser_free($this->parser);
            return 
    true;
        }
        function 
    startHandler($parser$name$attribs){
            if(!isset(
    $this->currTag[$name]))
            
    $this->currTag[$name] = array();
            
    $newTag = array();
            if(!empty(
    $attribs))
            
    $newTag['ATTR'] = $attribs;
            
    array_push($this->currTag[$name], $newTag); 
            
    $t =& $this->currTag[$name];
            
    $this->currTag =& $t[count($t)-1];
            
    array_push($this->tagStack$name);
        }
        function 
    dataHandler($parser$data){
        
    $data trim($data);
        if(!empty(
    $data)){
            if(isset(
    $this->currTag['DATA']))
                
    $this->currTag['DATA'] .= $data;
            else
                
    $this->currTag['DATA'] = $data;
            }
        }
        function 
    endHandler($parser$name){
            
    $this->currTag =& $this->document;
            
    array_pop($this->tagStack);
            for(
    $i 0$i count($this->tagStack); $i++){
                
    $t =& $this->currTag[$this->tagStack[$i]];
                
    $this->currTag =& $t[count($t)-1];
            }
        }

    Used simply like this:
    PHP Code:
    $d =& new XMLParser("file.xml");
    $d->parse();
    //Use DATA when getting value and ATTR for attribute.
    //Below shows 450.00
    echo($d->document['Property'][0]['Rent'][0]['DATA']);
    //Below shows Month
    echo($d->document['Property'][0]['Rent'][0]['ATTR']); 
    ... I believe thats how it works, I use it all the time but never for attributes... so I'm guessing that for attributes the "i" in ([i]['ATTR']) is the position of the attribute (u can test it and hope thats how it works, but this is the best class for xml in php4)... I think.
    Last edited by Templarian; January 28th, 2008 at 02:53 PM.

    Join #kirupa | Click the under my Username | CL | Jeff

  3. #3
    1,839
    posts
    Registered User
    yeah use a class for it...or better yet use simpleXML Object(but thats only available php5)

  4. #4
    thanks for the replies guys! am continuing trying different things and will post back when I get a neat solution that may be useful to others. thanks again! much appreciated!

  5. #5
    That class should work... I'm to lazy to test it but yea it should work perfectly I've used it numerous times at work on a php4 servers.

    Join #kirupa | Click the under my Username | CL | Jeff

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012