Library code snippets
How to create XML files
If you need to create XML files in PHP, you can do it without having to create the tags yourself with strings. This code shows you how to use the new_child functions to create an XML file.
<?
$doc = new_xmldoc('1.0');
$root = $doc->add_root('members');
$member = $root->new_child('member','');
$member->new_child('lastName','John');
$member->new_child('firstName','Adams');
$member->new_child('contribution','3400');
$member = $root->new_child('member','');
$member->new_child('lastName','Debra');
$member->new_child('firstName','Hones');
$member->new_child('contribution','2400');
$member = $root->new_child('member','');
$member->new_child('lastName','Jake');
$member->new_child('firstName','Tudor');
$member->new_child('contribution','1200');
$fp = @fopen('members.xml','w');
if(!$fp) {
die('Error cannot create XML file');
}
fwrite($fp,$doc->dumpmem());
fclose($fp);
?>
would create the following XML file.
<members>
<member>
<lastName>John</lastName>
<firstName>Adams</firstName>
<contribution>3400</contribution>
</member>
<member>
<lastName>Debra</lastName>
<firstName>Hones</firstName>
<contribution>2400</contribution>
</member>
<member>
<lastName>Jake</lastName>
<firstName>Tudor</firstName>
<contribution>1200</contribution>
</member>
</members>
Related articles
Related discussion
-
A particular gallery image
by margy80 (0 replies)
-
(Very urgent)how to assign the value of the variable in javascript function into php variable
by mazhar_qayyum (3 replies)
-
XML creating using php
by anandp_9 (0 replies)
-
free hosting with PHP & XML
by Wpodgorski (0 replies)
-
XML / SOAP PHP & C++
by weeb (0 replies)
Related podcasts
-
EarthClassMail.com - Moving from LAMP to .NET 3.5
Scott chats with Matt Davis, architect at EarthClassMail.com, about their move from a LAMP stack (Linux/Apache/mysql/PHP) to .NET 3.5. What's working, what's not, and what kinds of issues are they running into as their architect their solution.
Events coming up
-
Apr
17
WebTech Conference 2010 - Bulgaria
Veliko Turnovo, Bulgaria
6th edition of WebTech conference will be held. A 2 day conference about : - Web Technologies - Blogs and blogging - Web 3.0 - Open Web - Mobile technologies - Internet Business
i have the following error in line:
$doc = new_xmldoc('1.0');
when i am debbuging to create my xml file appears:
<b>Fatal error</b>: Class 'xmldoc' not found in <b>D:\Program Files\xampp\htdocs\dokimi\create_xml.php</b> on line <b>5</b><br />
why?? am i doing something wrong??
Thank you
I am a fairly new coder, at least when it come to XML and this php code. I had a couple of questions. I am trying to make my php script duplicate and XML example I was given. I followed your advice above but there where a couple of differences between the two. I was hoping you might help me out on how to fix them.
1) in the XML sheet that I am trying to duplicate each child object is on a new line and indented. Using your method above everything appears on one line. Does this matter? Is there a fix? Even if it doesn't matter it would be ideal to have each child object on a new line.
For example:
<root>
<child1>value1</child1>
<child2>value2<child2>
<childchild1>subvalue1</childchild1>
</root>
As Opposed to:
<root><child1>value1</child1><child2>value2<child2><childchild1>subvalue1</childchild1></root>
2)fields that are blank in my example show up as:
<notblank1>value1</notblank1>
<blank />
<notblank2>value2</notblank2>
After implementing your method they show up as:
<notblank1>value1</notblank1>
<blank></blank>
<notblank2>value2</notblank2>
I know I am asking a lot but I could really use the help. Thanks for any suggestions.
Jed
Hi, i was wondering do i have download XML DOM to be able to create an xml file, MY php parser does not recognise the xml functions ? Thanks in advance
This thread is for discussions of How to create XML files.