Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 55,398 times

Related Categories

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>

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.

Comments

  • Re: [3944] How to create XML files

    Posted by eagleman on 25 Feb 2007

    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 fou...

  • Re: [3944] How to create XML files

    Posted by jherzog on 19 May 2006

    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 fol...

  • Creating xml files using php

    Posted by Welshie on 30 Jan 2004

    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