Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.


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 ....

    <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 ??? :-)


  2. #2
    Here use this for PHP4:
    <?xml version="1.0" encoding="UTF-8"?>
            <Rent Per="Month">450.00</Rent>
    PHP Code:
    class XMLParser{
    $this->parser xml_parser_create();
    $this->filePath $path;
    $this->document = array();
    $this->currTag =& $this->document;
    $this->tagStack = array();
    $fp fopen($this->filePath"r"))){
    "Cannot open XML data file: $this->filePath");
    $data fread($fp4096)){
    sprintf("XML error: %s at line %d",
    $this->currTag[$name] = array();
    $newTag = array();
    $newTag['ATTR'] = $attribs;
    array_push($this->currTag[$name], $newTag); 
    $t =& $this->currTag[$name];
    $this->currTag =& $t[count($t)-1];
    $data trim($data);
    $this->currTag['DATA'] .= $data;
    $this->currTag['DATA'] = $data;
    $this->currTag =& $this->document;
    $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");
    //Use DATA when getting value and ATTR for attribute.
    //Below shows 450.00
    //Below shows Month
    ... 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
    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


Copyright 1999 - 2012