Link to home
Start Free TrialLog in
Avatar of mugsinc
mugsincFlag for Sri Lanka

asked on

How to create a simple xml file through php

How to create a simple xml file through php, simple date.
ASKER CERTIFIED SOLUTION
Avatar of jezweb
jezweb
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mugsinc

ASKER

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
if you use

$doc = new xmldoc('1.0');

instead of

$doc = new_xmldoc('1.0');

does it work?
Avatar of mugsinc

ASKER

nope, i get this error now

Fatal error: Class 'xmldoc' not found in D:\Hosting\5806313\html\xirr\create.php on line 13
Avatar of Beverley Portlock
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

Avatar of mugsinc

ASKER

bportlock, whats the file name it creates?
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.
Avatar of mugsinc

ASKER

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
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() );


Avatar of mugsinc

ASKER

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
Avatar of mugsinc

ASKER

i used a coldfusion and created a xml file in the same folder, but i cant replicate the same function in PHP
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.
Avatar of mugsinc

ASKER

i tried both and got the same error
Please post the code here.
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