Solved

How to create a simple xml file through php

Posted on 2010-08-25
15
620 Views
Last Modified: 2012-05-10
How to create a simple xml file through php, simple date.
0
Comment
Question by:mugsinc
  • 7
  • 5
  • 2
  • +1
15 Comments
 
LVL 3

Accepted Solution

by:
jezweb earned 500 total points
ID: 33519208
There's a lot of examples about this on the web, eg ref to
http://www.developerfusion.com/code/3944/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>

Open in new window

0
 
LVL 1

Author Comment

by:mugsinc
ID: 33519229
i get the below error when i run the file

Fatal error: Call to undefined function new_xmldoc() in D:\xxxx\xxxxx\html\xirr\create.php on line 13
0
 
LVL 3

Expert Comment

by:jezweb
ID: 33519296
if you use

$doc = new xmldoc('1.0');

instead of

$doc = new_xmldoc('1.0');

does it work?
0
 
LVL 1

Author Comment

by:mugsinc
ID: 33519345
nope, i get this error now

Fatal error: Class 'xmldoc' not found in D:\Hosting\5806313\html\xirr\create.php on line 13
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 33519348
You may not have the XML_DOM functions installed in PHP. However simpleXML is installed by default and is good enough for general use.

http://uk2.php.net/manual/en/book.simplexml.php


<?php

$xml = new SimpleXMLElement("<base></base>");

$xml->addChild("field1", "a value");
$xml->addChild("field2", "another value");
$xml->addChild("multi");
$xml->multi->addChild("Another", "1234");
$xml->multi->addChild("Another", "9876");

echo $xml->asXml();

// To show it "directly" in the browser we will use htmlentities

echo "<br/>";
echo htmlentities( $xml->asXml() );

Open in new window

0
 
LVL 1

Author Comment

by:mugsinc
ID: 33519374
bportlock, whats the file name it creates?
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 33519417
It does not create a file, it creates the XML in memory but it is easily written to a file using file_put_contents

   file_put_contents("anXmlFile.xml", $xml->asXml() );

http://www.php.net/file_put_contents


Unless you are creating *huge* amounts of XML then SimpleXML will work fine. If your XML is going to be (say) 10 MB in size then install the XML_WRITER or XML_DOM tools or write it directly to file yourself.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 1

Author Comment

by:mugsinc
ID: 33519441
this is the error i get when i try to create the file

Warning: file_put_contents(anXmlFile.xml) [function.file-put-contents]: failed to open stream: Permission denied in
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 33519510
That is not an XML error, it simply means that the web daemon (Apache) has no permission to write to whatever folder it is trying to use. Try putting in a full path

file_put_contents("/path/to/my/folder/anXmlFile.xml", $xml->asXml() );


0
 
LVL 1

Author Comment

by:mugsinc
ID: 33519530
Warning: file_put_contents(D:\Hosting806313\html\xirr\members.xml) [function.file-put-contents]: failed to open stream: Invalid argument in D:\Hosting\5806313\html\xirr\create.php on line 24
0
 
LVL 1

Author Comment

by:mugsinc
ID: 33519535
i used a coldfusion and created a xml file in the same folder, but i cant replicate the same function in PHP
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 33519645
Please post the code fragment you are using. Also in PHP path names use '/' rather than '\' even if you are on Windows. You cannot use '\' in a doublequoted PHP string as it acts as an escaping mechanism. PHP will translate the foward slashes to back slashes automatically.
0
 
LVL 1

Author Comment

by:mugsinc
ID: 33519700
i tried both and got the same error
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 33520006
Please post the code here.
0
 
LVL 5

Expert Comment

by:wmadrid1
ID: 33520009
mugsinc
You can try retireve the xml file without have the xml extension loaded in the server:


<?
echo '<?xml version="1.0" encoding="UTF-8" ?>';
echo '<Data>';
echo "<record att='nothing'></record>";
echo '</Data>';
?>

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now