Viewed   93 times

I need to create a new XML file and write that to my server. So, I am looking for the best way to create a new XML file, write some base nodes to it, save it. Then open it again and write more data.

I have been using file_put_contents() to save the file. But, to create a new one and write some base nodes I am not sure of the best method.




DOMDocument is a great choice. It's a module specifically designed for creating and manipulating XML documents. You can create a document from scratch, or open existing documents (or strings) and navigate and modify their structures.

$xml = new DOMDocument();
$xml_album = $xml->createElement("Album");
$xml_track = $xml->createElement("Track");
$xml_album->appendChild( $xml_track );
$xml->appendChild( $xml_album );


To re-open and write:

$xml = new DOMDocument();
$nodes = $xml->getElementsByTagName('Album') ;
if ($nodes->length > 0) {
   //insert some stuff using appendChild()

Monday, August 15, 2022

Seems like there is some redirection problem in the url you are accessing, and most probably simplexml_load_file() doesn't follow redirects...

So the solution would be to use file_get_contents() or cUrl...

As file_get_contents() is easier, I am showing that only...

The code would have to be something like:


    $xml = simplexml_load_string(file_get_contents(''));




    $xml = simplexml_load_string(file_get_contents(''));


^ That too, totally works!

Sunday, December 25, 2022

There is much simpler way of serializing objects, use XmlSerializer instead. See documentation here.

Code snippet to serialize your garage to file could look like:

var garage = new theGarage();

// TODO init your garage..

XmlSerializer xs = new XmlSerializer(typeof(theGarage));
TextWriter tw = new StreamWriter(@"c:tempgarage.xml");
xs.Serialize(tw, garage);

And code to load garage from file:

using(var sr = new StreamReader(@"c:tempgarage.xml"))
   garage = (theGarage)xs.Deserialize(sr);
Monday, September 12, 2022

To record in .csv is not necessary to use F# Data.

I changed the definition of test and added some values so that you can compile:

type test = { G:double; P:double; GG:double; PP:double }
            override this.ToString() = 
                sprintf "%f;%f;%f;%fn" this.G this.P this.GG this.PP

let G_0  =  [|(0.0)..(10.0)|]
let Un0  =  [|(1.0)..(11.0)|]
let P0   =  [|(2.0)..(12.0)|]
let G0   =  [|(3.0)..(13.0)|]
let PP0  =  [|(4.0)..(14.0)|]

let table = [for x in 0..(Un0.Length - 1) -> 
                let b = Un0.[x] 
                if b=0.0 then {G=0.0; P=0.0; GG=0.0; PP=0.0}
                else {G=G_0.[x]/b; P=P0.[x]/b; GG=G0.[x]/b; PP=PP0.[x]/b}]

let wr = new System.IO.StreamWriter("Csv.csv")
table |> |> String.concat("") |> wr.Write


Monday, October 3, 2022

I see from the comments that you're working from within a CMS framework and are unable to stop content from being output prior to where your code will be.

If the script in which you're working has already output content (beyond your control), then you can't do what you're trying to achieve in just one script.

Your script can either send headers saying "the following content is HTML" then output the HTML or send headers saying "the following content is XML, is an attachment and has a certain filename". You can't do both.

You can either output HTML containing a link to a separate script for downloading an XML file or you can issue a file download and output no HTML.

Therefore, you'll have to add a download link in the output of the CMS script you're modifying and then handle the download in a separate script.

I have made a working example that should help. The example includes a simple HTML document containing a download link, and a PHP script that then handles the download.

View the code below or take a look at the live example.

HTML (extraneous fluff removed, not necessarily valid)

<title>XML Download Example</title>


<a href="download.php">Download XML example</a>



// Populate XML document
    $doc = new DomDocument();
    // ... various modifications to the document are made

// Output headers
    header('Content-type: "text/xml"; charset="utf8"');
    header('Content-disposition: attachment; filename="example.xml"');

// Output content
    echo $doc->saveXML();

If you are fully unable to handle the download via a second script (perhaps you can't get access to the relevant data), you'll have to re-think the problem.

Tuesday, October 4, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :