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>

Comments

  1. 25 Feb 2007 at 11:14

    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

  2. 19 May 2006 at 06:33

    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





























  3. 30 Jan 2004 at 13:52

    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

  4. 01 Jan 1999 at 00:00

    This thread is for discussions of How to create XML files.

Leave a comment

Sign in or Join us (it's free).

Edward Tanguay Edward Tanguay updates his personal web site tanguay.info weekly with code, links, quotes and thoughts on web development. Sign up for the free newsletter.

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

Want to stay in touch with what's going on? Follow us on twitter!