Go Back   Dynamic Drive Forums > General Coding > PHP
Search Dynamic Drive Forums:

Reply
 
Thread Tools Search this Thread
  #1  
Old 10-20-2006, 11:44 AM
pavmoxo pavmoxo is offline
Regular Coders
 
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!!!
Reply With Quote
  #2  
Old 10-20-2006, 04:48 PM
pavmoxo pavmoxo is offline
Regular Coders
 
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??
Reply With Quote
  #3  
Old 10-25-2006, 07:46 AM
codeexploiter's Avatar
codeexploiter codeexploiter is offline
Elite Coders
 
Join Date: Sep 2005
Location: India
Posts: 1,633
Thanks: 6
Thanked 108 Times in 108 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.
__________________
-Codex
http://jpvalappil.wordpress.com/
Reply With Quote
  #4  
Old 10-25-2006, 09:18 AM
pavmoxo pavmoxo is offline
Regular Coders
 
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?
Reply With Quote
  #5  
Old 10-25-2006, 09:22 AM
codeexploiter's Avatar
codeexploiter codeexploiter is offline
Elite Coders
 
Join Date: Sep 2005
Location: India
Posts: 1,633
Thanks: 6
Thanked 108 Times in 108 Posts
Default

hmmmm i've developed it with PHP 5 i don't have PHP4 could you please test that it in your machine?
__________________
-Codex
http://jpvalappil.wordpress.com/
Reply With Quote
  #6  
Old 10-27-2006, 12:14 AM
boxxertrumps's Avatar
boxxertrumps boxxertrumps is offline
Senior Coders
 
Join Date: Jun 2006
Location: Acton Ontario Canada.
Posts: 681
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.
Reply With Quote
  #7  
Old 08-31-2007, 12:38 PM
mabedan mabedan is offline
New Comer (less than 5 posts)
 
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>
Reply With Quote
  #8  
Old 09-17-2007, 12:13 PM
coops2000 coops2000 is offline
Junior Coders
 
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.
Reply With Quote
  #9  
Old 09-17-2007, 02:52 PM
djr33's Avatar
djr33 djr33 is offline
Global Moderator
 
Join Date: Mar 2006
Location: N. California, USA
Posts: 8,300
Thanks: 57
Thanked 343 Times in 339 Posts
Default

Also, note this other current thread--
http://www.dynamicdrive.com/forums/s...ad.php?t=24776
__________________
Daniel - Freelance Web Design | <?php?> | <html>| Ich kann Deutsch. | Capisco l'italiano. | Estudio español. | Estudava português. | un peu de français | 日本語の学生です。| درست العربية
Reply With Quote
  #10  
Old 11-11-2008, 02:25 AM
vaccuchuoi vaccuchuoi is offline
New Comer (less than 5 posts)
 
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
Reply With Quote
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:45 AM.

Home - Contact Us - Archives - Link to DD - Top 

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.