generating xml document

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

generating xml document

Post 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
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.');
}
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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?
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post 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.
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post 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()));
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

itsmani1 wrote:its prints it but don't save.
Why should it?
What does the manual say about savexml()?
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

i don't know how to create attributes using dom i am trying without success:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

volka wrote:You can set an attribute via DOMElement->setAttribute(), see http://de2.php.net/dom
What have you tried so far?
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post 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">
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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...
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Try setAttributeNS instead of createAttributeNS, you don't have to append then either.
Post Reply