Re: please help me about xml..

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



XML manipulation is really simple with PHP 5's native DOM and SimpleXML 
extensions.

I use the SimpleXML extension for parsing an XML file, and the DOM 
extension for creating XML files and/or writing to already existing files.

For example, if this is your XML file:

<?xml version="1.0" encoding="UTF-8"?>
<users>
   <user id="1" joindate="1102487272">
     <username>rks</username>
     <password>7fd7203caaabcc18bd94d1cf03afd12ddb8f8ad7</password>
     <name>Rajesh Kumar</name>
   </user>
   <user id="2" joindate="1101878008">
     <username>rajesh</username>
     <password>mypass</password>
     <name>Rajesh Kumar</name>
   </user>
   <user id="3" joindate="1101878008">
     <username>benjamin</username>
     <password>1161e6ffd3637b302a5cd74076283a7bd1fc20d3</password>
     <name>Benjamin Myas</name>
   </user>
</users>

You can read this XML file very easily using SimpleXML:

<?php

$xml = simplexml_load_file('users.xml');

foreach ($xml as $key => $val) {

     echo 'Userid: ' . $val['id'] . "\n";
     echo 'Username: ' . $val->username . "\n";
     echo 'Name: ' . $val->name . "\n";

     echo "\n";

}

?>

In other words, your XML elements become properties, while your 
attributes become keys (or indexes).

Similarly, if you want to create the same XML document above, you could 
use the DOM extension. So say you had a PHP array that you got from a 
database that looks like this:

$users_array = array(array('id' => 1,
                            'username' => 'rks',
                            'first_name' => 'Rajesh',
                            'last_name' => 'Kumar'),
                      array('id' => 2,
                            'username' => 'benjamin',
                            'first_name' => 'Benjamin',
                            'last_name' => 'Myas'));

then, you could transform this array into XML like so:

<?php

$dom = new DOMDocument('1.0');
$users = $dom->appendChild($dom->createElement('users'));

foreach ($users_array as $user_array) {

     $user = $users->appendChild($dom->createElement('user'));

     foreach ($user_array as $key => $val) {

         if ($key == 'id') {

             // say our app requires ids to be stored as attributes		
             $user->setAttribute('id', $user_array['id']);

         } elseif ($key == 'username') {

             // let's store the username as CDATA because according to
             // our app, it can contain special chars

             $CDataNode = $user->appendChild($dom->createElement($key));
             $CDataNode->appendChild($dom->createCDATASection($val));

         } else {

             // regular text nodes

             $textNode = $user->appendChild($dom->createElement($key));
             $textNode->appendChild($dom->createTextNode($val));

         }

     }

}

$dom->formatOutput = true;
echo $dom->saveXML();

?>

--
Rajesh

z.shafiee wrote:
> hi..
> i dont know how xml work!
> i see functions for parssing xml with php4 and php5 but not enough.
> i need more information about startElement and endElement and 
> characterData functions.
> please help me.
> thanks.


PHP Data object relational mapping generator
http://www.metastorage.net/ 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/php-objects/

<*> To unsubscribe from this group, send an email to:
    php-objects-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Soap]     [Kernel Newbies]     [Yosemite]     [Yosemite Campsites]

  Powered by Linux