February 26, 2012

Php that generates XML

Question by dane rias

<?php

require("phpsqlajax_dbinfo.php");
$dom  = new DOMDocument("1.0");
$dp   = fopen('samp.xml', 'w');
$node = $dom->createElement("Groceries");
fwrite($dp, '$node');
$parnode = $dom->appendChild($node);

$connection = mysql_connect($host, $user, $pass);
if (!$connection) {
    die('Not connected : ' . mysql_error());
}

$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
    die ('Can't use db : ' . mysql_error());
}

$query  = "SELECT * FROM tbl_groceryitem";
$result = mysql_query($query);
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

echo "<groceries>";
while ($row = @mysql_fetch_assoc($result)) {

    $node = $dom->createElement("item");
    echo "<echo>";
    fwrite($dp, '$node');
    $newnode = $parnode->appendChild($node);
    $newnode->setAttribute("auto_id", $row['auto_id']);
    echo "<auto_id>", $row[auto_id];
    fwrite($dp, '$newnode');
    $newnode->setAttribute("Gro_barcode", $row['Gro_barcode']);
    echo "<Gro_barcode>", $row[Gro_barcode];
    fwrite($dp, '$newnode');
    $newnode->setAttribute("Gro_name", $row['Gro_name']);
    echo "<Gro_name>", $row[Gro_name];
    fwrite($dp, '$newnode');
    $newnode->setAttribute("Gro_brand", $row['Gro_brand']);
    echo "<Gro_brand>", $row[Gro_brand];
    fwrite($dp, '$newnode');
    $newnode->setAttribute("Gro_category", $row['Gro_category']);
    echo "<Gro_category>", $row[Gro_category];
    fwrite($dp, '$newnode');
    $newnode->setAttribute("Gro_aisle", $row['Gro_aisle']);
    echo "<Gro_category>", $row[Gro_aisle];
    fwrite($dp, '$newnode');
    $newnode->setAttribute("Gro_qty", $row['Gro_qty']);
    echo "<Gro_qty>", $row[Gro_qty];
    fwrite($dp, '$newnode');
    $newnode->setAttribute("Gro_netwt", $row['Gro_netwt']);
    echo "<Gro_netwt>", $row[Gro_netwt];
    fwrite($dp, '$newnode');
    $newnode->setAttribute("Gro_pic", $row['Gro_pic']);
    echo "<Gro_pic>", $row[Gro_pic];
    fwrite($dp, '$newnode');
    $newnode->setAttribute("Gro_price", $row['Gro_price']);
    echo "<Gro_price>", $row[Gro_price];
    fwrite($dp, '$newnode');
    $newnode->setAttribute("Gro_tax", $row['Gro_tax']);
    echo "<Gro_tax>", $row[Gro_tax];
    fwrite($dp, '$newnode');
    echo "</item>";
    fwrite($dp, '</item>');
}
fwrite($dp, '</groceries');
echo $dom->saveXML($xml);

?>

I’m new in php.

I’m creating a php file that can generate xml file with data from myPHPAdmin. Thanks :D. Hope someone can help me.

At first try, the code has been displayed in php and when I open to check for the created XML. the display was ‘$node‘, it reflects the exact string a place inside fwrite, when I try to delete the string quote (”) like this fwrite($dp,$node);. I got error.

And when I try to return the code to fwrite($dp,’$node’);. There was no Display. the XML page is blank.

Answer by Starx

You need to use XML headers when you are creating a XML file with PHP.

<?xml version="1.0" encoding="utf-8"?>

But this is not needed when you are using saveXML() on the end.

Here is an example, picked out from php’s manual.

<?php

$doc = new DOMDocument('1.0');
// we want a nice output
$doc->formatOutput = true;

$root = $doc->createElement('book');
$root = $doc->appendChild($root);

$title = $doc->createElement('title');
$title = $root->appendChild($title);

$text = $doc->createTextNode('This is the title');
$text = $title->appendChild($text);

echo "Saving all the document:n";
echo $doc->saveXML() . "n";

echo "Saving only the title part:n";
echo $doc->saveXML($title);
?>

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!