generating xml document
Moderator: General Moderators
- itsmani1
- Forum Regular
- Posts: 791
- Joined: Mon Sep 29, 2003 2:26 am
- Location: Islamabad Pakistan
- Contact:
generating xml document
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
You need headers:
And then you just echo you xml out to the reader.
Code: Select all
header("Cache-control: private");
header("Content-type: text/xml");So many possibilites, e.g.
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.');
}- itsmani1
- Forum Regular
- Posts: 791
- Joined: Mon Sep 29, 2003 2:26 am
- Location: Islamabad Pakistan
- Contact:
how can i add attributes to this like if I need to add :
what should i do?
and how to save it as well,
**************************
the method you posted is dom, does php support some other method.
Code: Select all
<QBXMLMsgsRq onError="continueOnError">and how to save it as well,
**************************
the method you posted is dom, does php support some other method.
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.
- itsmani1
- Forum Regular
- Posts: 791
- Joined: Mon Sep 29, 2003 2:26 am
- Location: Islamabad Pakistan
- Contact:
I am using this code to append elements to xml but problem is that its not saving the file. its prints it but don't save.
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()));What have you tried so far?volka wrote:You can set an attribute via DOMElement->setAttribute(), see http://de2.php.net/dom
- itsmani1
- Forum Regular
- Posts: 791
- Joined: Mon Sep 29, 2003 2:26 am
- Location: Islamabad Pakistan
- Contact:
with the following XML
Using this code:
I am getting this output
but i don't know how to set surname's values
i want : <employee surname="my surname">
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>
i want : <employee surname="my surname">
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Try setAttributeNS instead of createAttributeNS, you don't have to append then either.