0

I have the following xml being returned when using fsockopen (shortened the xml for ease of reading):

<car>
  <brand type="AUDI">
    <engine>1.8</engine>
    <price>9000</price>
  </brand>
</car>

I use the following code to get the values:

    $p = xml_parser_create();
    xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
    xml_parser_set_option($p, XML_OPTION_TARGET_ENCODING, 'UTF-8'); 
    xml_parse_into_struct($p, $return_xml, $vals, $index);
    xml_parser_free($p);

    $return_array["engine"] = $vals[$index["engine"][0]]["value"];
    $return_array["price"] = $vals[$index["price"][0]]["value"];        

Which gives me the engine, and price but how can I get the value brand type?

Thanks

Dino
  • 1,445
  • 4
  • 24
  • 43
  • if you are not fixed on using the rather complicated xml_parser extension have a look at http://stackoverflow.com/questions/4906073/a-simple-program-to-crud-node-and-node-values-of-xml-file and http://stackoverflow.com/questions/3577641/best-methods-to-parse-html/3577662#3577662 – Gordon Dec 07 '11 at 15:36

1 Answers1

1

xml_parser is the oldest and cruddiest of the XML implementations in PHP. I would recommend using SimpleXML, as it's OO oriented and a bit easier to use.

With SimpleXML you could do:

$car->brand[0]['type'];

Check it out:

http://www.php.net/manual/en/simplexml.examples-basic.php

Jonathan Rich
  • 1,740
  • 10
  • 11