Page 1 of 1

including attributes in XML

Posted: Fri Jul 09, 2010 3:01 pm
by swan52
I am trying to use PHP to read xml data such as this, however after much googling it has become apparent that such information is hard to come by. Eventually I found a very helpful code to convert the data into an array but unfortunately were I to use it on the example xml below it misses the attributes, name, country and population.

cities.xml

Code: Select all

<?xml version="1.0"?>

<resultset statement="SELECT * FROM xmltest.cities ORDER BY name"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <row>
        <field name="name">Ciudad de Méico</field>
        <field name="country">Mexico</field>
        <field name="population">8591309</field>
  </row>

  <row>
        <field name="name">Istanbul</field>
        <field name="country">Turkey</field>
        <field name="population">8787958</field>
  </row>

  <row>
        <field name="name">Jakarta</field>
        <field name="country">Indonesia</field>
        <field name="population">9604900</field>
  </row>

  <row>
        <field name="name">Karachi</field>
        <field name="country">Pakistan</field>
        <field name="population">9269265</field>
  </row>

  <row>
        <field name="name">Moscow</field>
        <field name="country">Russian Federation</field>
        <field name="population">8389200</field>
  </row>

  <row>
        <field name="name">Mumbai (Bombay)</field>
        <field name="country">India</field>
        <field name="population">10500000</field>
  </row>

  <row>
        <field name="name">New York</field>
        <field name="country">United States</field>
        <field name="population">8008278</field>
  </row>

  <row>
        <field name="name">São Paulo</field>
        <field name="country">Brazil</field>
        <field name="population">9968485</field>
  </row>

  <row>
        <field name="name">Seoul</field>
        <field name="country">South Korea</field>
        <field name="population">9981619</field>
  </row>

  <row>
        <field name="name">Shanghai</field>
        <field name="country">China</field>
        <field name="population">9696300</field>
  </row>
</resultset>
PHP code.

Code: Select all

<?php

// array function convert
function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
    $arrData = array();
   
    // if input is object, convert into array
    if (is_object($arrObjData)) {
        $arrObjData = get_object_vars($arrObjData);
    }
   
    if (is_array($arrObjData)) {
        foreach ($arrObjData as $index => $value) {
            if (is_object($value) || is_array($value)) {
                $value = objectsIntoArray($value, $arrSkipIndices); // recursive call
            }
            if (in_array($index, $arrSkipIndices)) {
                continue;
            }
            $arrData[$index] = $value;
        }
    }
    return $arrData;
}
?>

<?php
$xmlUrl = "cities.xml"; // XML feed file/URL
$xmlStr = file_get_contents($xmlUrl);
$xmlObj = simplexml_load_string($xmlStr);
$arrXml = objectsIntoArray($xmlObj);
print_r($arrXml);
?>
Array ( [field] => Array ( [0] => Ciudad de Méico [1] => Mexico [2] => 8591309 ) )

name?
country?
population?

How would I include this information?

Re: including attributes in XML

Posted: Fri Jul 09, 2010 3:20 pm
by websitesca
First, if you're using php5, you want to be using libxml2. Make sure that is enabled using phpinfo().

DOM and SimpleXML are probably the easiest ways for you to proceed.

I like SimpleXML - I know you're probably excited about using an array because you're familiar with it, but the object model of SimpleXML is way better.

For xml code that looks like this:

Code: Select all

$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies>
 <movie>
  <title>PHP: Behind the Parser</title>
  <characters>
   <character>
    <name>Ms. Coder</name>
    <actor>Onlivia Actora</actor>
   </character>
   <character>
    <name>Mr. Coder</name>
    <actor>El Act&#211;r</actor>
   </character>
  </characters>
  <plot>
   So, this language. It's like, a programming language. Or is it a
   scripting language? All is revealed in this thrilling horror spoof
   of a documentary.
  </plot>
  <great-lines>
   <line>PHP solves all my web problems</line>
  </great-lines>
  <rating type="thumbs">7</rating>
  <rating type="stars">5</rating>
 </movie>
</movies>
XML;
Then all you have to do is this:

Code: Select all

$xml = new SimpleXMLElement($xmlstr);
And then you can do stuff that is as powerful as this:

Code: Select all

echo $xml->movie[0]->plot;
I got this info from here:
http://www.php.net/manual/en/simplexml. ... -basic.php

Hope that helps!
Georges,
http://www.websites.ca

Re: including attributes in XML

Posted: Fri Jul 09, 2010 6:27 pm
by swan52
That's just made things unbelievably easy, I've been searching for weeks for something like this... Wow thanks so much.