generating xml document
Posted: Mon Dec 04, 2006 7:49 am
how can i generate xml document, i know how to parse xml document but i wanted to generate xml document from database.
any help
any help
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
header("Cache-control: private");
header("Content-type: text/xml");Code: Select all
<?php
$sameAsWithDB = array(
array('forename'=>'Chico', 'surname'=>'Marx'),
array('forename'=>'Harpo', 'surname'=>'Marx'),
array('forename'=>'Groucho', 'surname'=>'Marx')
);
$dom = new DOMDocument();
$root = $dom->appendChild($dom->createElement('MarxBrothers'));
foreach($sameAsWithDB as $row) {
$b = $dom->createElement('person');
foreach($row as $name=>$value) {
$b->appendChild($dom->createElement($name, $value));
}
$root->appendChild($b);
}
if (!headers_sent()) {
header('Content-type: text/xml');
echo $dom->saveXML();
}
else {
die('headers already sent.');
}Code: Select all
<QBXMLMsgsRq onError="continueOnError">You can set an attribute via DOMElement->setAttribute(), see http://de2.php.net/domitsmani1 wrote:how can i add attributes to this like if I need to add :
what should i do?Code: Select all
<QBXMLMsgsRq onError="continueOnError">
There's a method described in the manual asitsmani1 wrote:and how to save it as well,
http://de2.php.net/dom wrote:Dumps the internal XML tree back into a file
You can write the xml "by hand" if you like. Why, is there something wrong with dom?itsmani1 wrote:the method you posted is dom, does php support some other method.
Code: Select all
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->load ("axml.xml");
$dom->formatOutput = true;
$new_tag = $dom->createElement ('testNode');
$new_tag->appendChild (
$dom->createElement ('test', 'this is a test'));
$dom->documentElement->appendChild ($new_tag);
printf ("<pre>%s</pre>", htmlentities ($dom->saveXML()));Why should it?itsmani1 wrote:its prints it but don't save.
What have you tried so far?volka wrote:You can set an attribute via DOMElement->setAttribute(), see http://de2.php.net/dom
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<emp>
<employee>
<employeename>test</employeename>
<employeeaddress>test123</employeeaddress>
<SSN>12343456</SSN>
<company>TOPXML</company>
</employee>
</emp>Code: Select all
$doc = new DomDocument;
// Load the xml file into DOMDocument
$doc->Load('xx.xml');
// We retrieve the attribute named id of the employee element
$employee = $doc->getElementsByTagName('employee')->item(0);
//Create a attribute with namespace
$newattr = $doc ->createAttributeNS('ns','surname');
//append the new attribute node into the employee element
$employee -> appendChild($newattr);
//save the DOMDocument into a file
$test = $doc->save("save1.xml");
echo "<B>DOMCharacterData->createAttributeNS example<B>";Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<emp xmlns="ns">
<employee surname="">
<employeename>test</employeename>
<employeeaddress>test123</employeeaddress>
<SSN>12343456</SSN>
<company>TOPXML</company>
</employee>
</emp>
In technical terms you want to Set a surname Attribute to the employee node... Given the fact that volka already referred to the specific function i don't see how we can make it any clearer...itsmani1 wrote:but i don't know how to set surname's values
i want : <employee surname="my surname">