xmlreader attribute info

Discussion in 'PHP' started by ched, Nov 25, 2011.

  1. #1
    I am trying to read a large XML file and add it to a database.
    Here is a very small example of the XML:
    <?xml version="1.0" encoding="UTF-8"?>
    <shows>
       <show id="33041">
           <title>The Sound of Music</title>
           <category>Musicals</category>
           <price currency="GBP">
               <min_price>12</min_price>
               <max_price>42</max_price>
           </price>
       </show>
    </shows>
    PHP:
    The problem is that I do not know how to refer to the id value in <show id="33041">

    Here is a snippet of code I am using:
        $xml = new XMLReader();
        $xml->open(Test.xml);
        while ($xml->read()) {
    	        switch ($xml->name) {
    			case "event":
                $xml->read();
                $conf["event"] = $xml->value;
                $xml->read();
                break;
    			}
    	}
    PHP:
    So how on earth do I get the id bit????

    Thanks for any help.
     
    ched, Nov 25, 2011 IP
  2. thinglet

    thinglet Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $arr = array();
    $xml = simplexml_load_file('Test.xml');
    foreach($xml->show as $show) {
      $show_attributes = $show->attributes();
      $arr[] = $show_attributes['id'];
    }
    print_r($arr);
    Code (markup):
    $arr is an array populated with the id's of each <show> within <shows>
     
    thinglet, Nov 26, 2011 IP
  3. ched

    ched Active Member

    Messages:
    185
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #3
    Thanks very much. That helps for small files but some of the XML I have are 500mb!
    Thanks your code will help me.
     
    ched, Nov 26, 2011 IP