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!!!
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!!!
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??
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>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.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>";
}
?>
Writing XML using PHP
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.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")
?>
As i don't know much about perl it is not wise to comment about that. I think PHP is enough for this purpose.
But this solution works with PHP 4? or just for PHP 5?
hmmmm i've developed it with PHP 5 i don't have PHP4 could you please test that it in your machine?
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>
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>
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
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
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