Page 1 of 4 123 ... LastLast
Results 1 to 10 of 31

Thread: Read and write XML with PHP

  1. #1
    Join Date
    Apr 2006
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool Read and write XML with PHP

    Hi guys!!!

    I need some examples or an explanation for read some data of a XML file and for write some elements in the file with a form everything with PHP. Can You help me??

    Thanks!!!

  2. #2
    Join Date
    Apr 2006
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    The server that I want to put my page support PERL 5.8.7 and PHP 4.4.2.. What's the better language the read/write of/on a XML script without a DB in your opinion?? Can I choose one of them??

  3. #3
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,626
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Reading an XML File using PHP

    HTML Code:
    XML Code: file name - employees.xml
    
    <?xml version="1.0" encoding="iso-8859-1"?>
    <employees>
      <employee>
      <name>Mark</name>
      <age>27</age>
      <salary>$5000</salary>
      </employee>
      <employee>
      <name>Jack</name>
      <age>25</age>
      <salary>$4000</salary>
      </employee>
      </employees>
    PHP Code:
    <?php
    $doc 
    = new DOMDocument();
    $doc->load'employees.xml' );
      
    $employees $doc->getElementsByTagName"employee" );
    foreach( 
    $employees as $employee )
    {
      
    $names $employee->getElementsByTagName"name" );
      
    $name $names->item(0)->nodeValue;
      
      
    $ages$employee->getElementsByTagName"age" );
      
    $age$ages->item(0)->nodeValue;
      
      
    $salaries $employee->getElementsByTagName"salary" );
      
    $salary $salaries->item(0)->nodeValue;
      
      echo 
    "<b>$name - $age - $salary\n</b><br>";
      }
    ?>
    With the above XML file the PHP code will read the XML file and retrieve the information from it and output that in the screen.

    Writing XML using PHP

    PHP Code:
    <?php
      $employees 
    = array();
      
    $employees [] = array(
      
    'name' => 'Albert',
      
    'age' => '34',
      
    'salary' => "$10000"
      
    );
      
    $employees [] = array(
      
    'name' => 'Claud',
      
    'age' => '20',
      
    'salary' => "$2000"
      
    );
      
      
    $doc = new DOMDocument();
      
    $doc->formatOutput true;
      
      
    $r $doc->createElement"employees" );
      
    $doc->appendChild$r );
      
      foreach( 
    $employees as $employee )
      {
      
    $b $doc->createElement"employee" );
      
      
    $name $doc->createElement"name" );
      
    $name->appendChild(
      
    $doc->createTextNode$employee['name'] )
      );
      
    $b->appendChild$name );
      
      
    $age $doc->createElement"age" );
      
    $age->appendChild(
      
    $doc->createTextNode$employee['age'] )
      );
      
    $b->appendChild$age );
      
      
    $salary $doc->createElement"salary" );
      
    $salary->appendChild(
      
    $doc->createTextNode$employee['salary'] )
      );
      
    $b->appendChild$salary );
      
      
    $r->appendChild$b );
      }
      
      echo 
    $doc->saveXML();
      
    $doc->save("write.xml")
      
    ?>
    This code will output the XML file in console as well as it will create a XML file in the name of write.xml in the same directory of the PHP script.

    As i don't know much about perl it is not wise to comment about that. I think PHP is enough for this purpose.

  4. #4
    Join Date
    Apr 2006
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    But this solution works with PHP 4? or just for PHP 5?

  5. #5
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,626
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    hmmmm i've developed it with PHP 5 i don't have PHP4 could you please test that it in your machine?

  6. #6
    Join Date
    Jun 2006
    Location
    Acton Ontario Canada.
    Posts
    677
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Heres my conversion...

    <?php
    $doc = new DOMDocument();
    $doc->load( 'info.xml' );

    $blog = $doc->getElementsByTagName( "post" );
    foreach( $blog as $post )
    {
    $headers = $post->getElementsByTagName( "header" );
    $header = $headers->item(0)->nodeValue;

    $dates= $post->getElementsByTagName( "date" );
    $date= $dates->item(0)->nodeValue;

    $notes = $post->getElementsByTagName( "note" );
    $note = $notes->item(0)->nodeValue;

    $links = $post->getElementsByTagName( "link" );
    $link = $links->item(0)->nodeValue;

    echo '
    <div class="blog">$header</div>
    <div class="small">$date</div>
    <div class="note">$note</div>
    <a href="$link">$link</a> ';
    }
    ?>

    XML doc...

    <blog>
    <post>
    <header></header>
    <date></date>
    <note></note>
    <link></link>
    </post>
    </blog>
    - Ryan "Boxxertrumps" Trumpa
    Come back once it validates: HTML, CSS, JS.

  7. #7
    Join Date
    Aug 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default i can't use this

    i create the exact xml and php files that you've written there, but when i test them on my site, i get an error like this

    Parse error: parse error, unexpected T_OBJECT_OPERATOR in /homepages/1/d206546483/htdocs/jussieutik/test/tres.php on line 15

    my php is like this, is this the problem??
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Untitled Document</title>
    </head>
    <?php 
    $doc = new DOMDocument(); 
    $doc->load( 'employees.xml' ); 
       
    $employees = $doc->getElementsByTagName( "employee" ); 
    foreach( $employees as $employee ) 
    { 
      $names = $employee->getElementsByTagName( "name" ); 
      $name = $names->item(0)->nodeValue; 
       
      $ages= $employee->getElementsByTagName( "age" ); 
      $age= $ages->item(0)->nodeValue; 
       
      $salaries = $employee->getElementsByTagName( "salary" ); 
      $salary = $salaries->item(0)->nodeValue; 
       
      echo "<b>$name - $age - $salary\n</b><br>"; 
      } 
    ?>
    <body>
    </body>
    </html>

  8. #8
    Join Date
    Sep 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default domdocument() expects at least 1

    I've been trying to get a php class to write into xml... I've looked around and all the code seems the same to the below and everyone i have tried is coming up with the same error msg..

    Warning: domdocument() expects at least 1 parameter, 0

    Has anyone else had this problem? Does anyone know of an answer?

    Thanks

    Quote Originally Posted by codeexploiter View Post
    Reading an XML File using PHP

    HTML Code:
    XML Code: file name - employees.xml
    
    <?xml version="1.0" encoding="iso-8859-1"?>
    <employees>
      <employee>
      <name>Mark</name>
      <age>27</age>
      <salary>$5000</salary>
      </employee>
      <employee>
      <name>Jack</name>
      <age>25</age>
      <salary>$4000</salary>
      </employee>
      </employees>
    PHP Code:
    <?php
    $doc 
    = new DOMDocument();
    $doc->load'employees.xml' );
      
    $employees $doc->getElementsByTagName"employee" );
    foreach( 
    $employees as $employee )
    {
      
    $names $employee->getElementsByTagName"name" );
      
    $name $names->item(0)->nodeValue;
      
      
    $ages$employee->getElementsByTagName"age" );
      
    $age$ages->item(0)->nodeValue;
      
      
    $salaries $employee->getElementsByTagName"salary" );
      
    $salary $salaries->item(0)->nodeValue;
      
      echo 
    "<b>$name - $age - $salary\n</b><br>";
      }
    ?>
    With the above XML file the PHP code will read the XML file and retrieve the information from it and output that in the screen.

    Writing XML using PHP

    PHP Code:
    <?php
      $employees 
    = array();
      
    $employees [] = array(
      
    'name' => 'Albert',
      
    'age' => '34',
      
    'salary' => "$10000"
      
    );
      
    $employees [] = array(
      
    'name' => 'Claud',
      
    'age' => '20',
      
    'salary' => "$2000"
      
    );
      
      
    $doc = new DOMDocument();
      
    $doc->formatOutput true;
      
      
    $r $doc->createElement"employees" );
      
    $doc->appendChild$r );
      
      foreach( 
    $employees as $employee )
      {
      
    $b $doc->createElement"employee" );
      
      
    $name $doc->createElement"name" );
      
    $name->appendChild(
      
    $doc->createTextNode$employee['name'] )
      );
      
    $b->appendChild$name );
      
      
    $age $doc->createElement"age" );
      
    $age->appendChild(
      
    $doc->createTextNode$employee['age'] )
      );
      
    $b->appendChild$age );
      
      
    $salary $doc->createElement"salary" );
      
    $salary->appendChild(
      
    $doc->createTextNode$employee['salary'] )
      );
      
    $b->appendChild$salary );
      
      
    $r->appendChild$b );
      }
      
      echo 
    $doc->saveXML();
      
    $doc->save("write.xml")
      
    ?>
    This code will output the XML file in console as well as it will create a XML file in the name of write.xml in the same directory of the PHP script.

    As i don't know much about perl it is not wise to comment about that. I think PHP is enough for this purpose.

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Also, note this other current thread--
    http://www.dynamicdrive.com/forums/s...ad.php?t=24776
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. #10
    Join Date
    Nov 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default i have problem with this !

    i try to write a xml file but i have error with :
    "Warning: domdocument::domdocument() expects at least 1 parameter, 0 given in C:\xampp\htdocs\demo_ghifilexml\ghifilexml.php on line 2

    Fatal error: Call to undefined method domdocument::load() in C:\xampp\htdocs\demo_ghifilexml\ghifilexml.php on line 3
    "
    i copy your code and paste in my php text so i understand what happen.
    my php version:5.2.6

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •