Page 1 of 1

generating xml document

Posted: Mon Dec 04, 2006 7:49 am
by itsmani1
how can i generate xml document, i know how to parse xml document but i wanted to generate xml document from database.

any help

Posted: Mon Dec 04, 2006 8:09 am
by neophyte
You need headers:

Code: Select all

header("Cache-control: private");
    header("Content-type: text/xml");
And then you just echo you xml out to the reader.

Posted: Mon Dec 04, 2006 8:11 am
by volka
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.');
}

Posted: Mon Dec 04, 2006 11:40 pm
by itsmani1
how can i add attributes to this like if I need to add :

Code: Select all

 <QBXMLMsgsRq onError="continueOnError">
what should i do?

and how to save it as well,
**************************

the method you posted is dom, does php support some other method.

Posted: Mon Dec 04, 2006 11:52 pm
by volka
itsmani1 wrote:how can i add attributes to this like if I need to add :

Code: Select all

 <QBXMLMsgsRq onError="continueOnError">
what should i do?
You can set an attribute via DOMElement->setAttribute(), see http://de2.php.net/dom
itsmani1 wrote:and how to save it as well,
There's a method described in the manual as
http://de2.php.net/dom wrote:Dumps the internal XML tree back into a file
itsmani1 wrote:the method you posted is dom, does php support some other method.
You can write the xml "by hand" if you like. Why, is there something wrong with dom?

Posted: Tue Dec 05, 2006 12:22 am
by itsmani1
well,
i was looking into MS.net xml parsing and writing functions and there i saw DOM, Push model and Pull model that's why i asked if PHP also supports this?

anyway thanks for the help.

Posted: Sat Dec 09, 2006 7:05 am
by itsmani1
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()));

Posted: Sat Dec 09, 2006 8:53 am
by volka
itsmani1 wrote:its prints it but don't save.
Why should it?
What does the manual say about savexml()?

Posted: Tue Dec 19, 2006 2:14 am
by itsmani1
i don't know how to create attributes using dom i am trying without success:

Posted: Tue Dec 19, 2006 4:08 am
by volka
volka wrote:You can set an attribute via DOMElement->setAttribute(), see http://de2.php.net/dom
What have you tried so far?

Posted: Mon Dec 25, 2006 5:21 am
by itsmani1
with the following XML

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>
Using this code:

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>";
I am getting this output

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>
but i don't know how to set surname's values
i want : <employee surname="my surname">

Posted: Mon Dec 25, 2006 5:37 am
by timvw
itsmani1 wrote:but i don't know how to set surname's values
i want : <employee surname="my surname">
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...

Posted: Mon Dec 25, 2006 7:43 am
by Ollie Saunders
Try setAttributeNS instead of createAttributeNS, you don't have to append then either.