tags:

views:

2477

answers:

7

HI.......... i have to generate a xml file dynamically at runtime........So plz help me.

<?xml version="1.0" encoding="UTF-8"?>
   2.    <xml>
   3.     <track>
   4.         <path>song1.mp3</path>
   5.         <title>Track 1 - Track Title</title>
   6.     </track>
   7.     <track>
   8.         <path>song2.mp3</path>
   9.         <title>Track 2 - Track Title</title>
  10.     </track>
  11.     <track>
  12.         <path>song3.mp3</path>
  13.         <title>Track 3 - Track Title</title>
  14.     </track>
  15.     <track>
  16.         <path>song4.mp3</path>
  17.         <title>Track 4 - Track Title</title>
  18.     </track>
  19.     <track>
  20.         <path>song5.mp3</path>
  21.         <title>Track 5 - Track Title</title>
  22.     </track>
  23.     <track>
  24.         <path>song6.mp3</path>
  25.         <title>Track 6 - Track Title</title>
  26.     </track>
  27.     <track>
  28.         <path>song7.mp3</path>
  29.         <title>Track 7 - Track Title</title>
  30.     </track>
  31.     <track>
  32.         <path>song8.mp3</path>
  33.         <title>Track 8 - Track Title</title>
  34.     </track>
  35.    
  36.   </xml>

plz help me in generating above xml file dynamically using php...........

+2  A: 

You could use the xmlWriter class for this. On the site, there's some sample code for it, too.

schnaader
thn x..........it helped me....
musicking123
A: 

Can u please provide me the sample code?

musicking123
dude, follow the link
Natrium
giv me teh codez!!
Josh Smeaton
A: 

THN x...........i got it.

musicking123
@musicking123: This **is not a forum**. Its a question and answer site. You should make simple and to the point questions, and provide new *information* on the question by editing the question. Answers are just for that, asnwers. **Do not post** answers just saying thank you or *please send code*. The former belong on comments (like this one! :) and the latter is frowned upon. Read the http://stackoverflow.com/faq
voyager
+2  A: 

an easy way to do this is :

<?php
// Send the headers
header('Content-type: text/xml');
header('Pragma: public');
header('Cache-control: private');
header('Expires: -1');
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>";

echo '<xml>';

// echo some dynamically generated content here
/*
<track>
    <path>song_path</path>
    <title>track_number - track_title</title>
</track>
*/

echo '</xml>';

?>

save it as .php

andyk
A: 

Take a look at the Tiny But Strong templating system. It's generally used for templating HTML but there's an extension that works with XML files. I use this extensively for creating reports where I can have one code file and two template files - htm and xml - and the user can then choose whether to send a report to screen or spreadsheet.

Another advantage is you don't have to code the xml from scratch, in some cases I've been wanting to export very large complex spreadsheets, and instead of having to code all the export all that is required is to save an existing spreadsheet in xml and substitute in code tags where data output is required. It's a quick and a very efficient way to work.

Cruachan
+5  A: 

I'd use SimpleXMLElement.

<?php

$xml = new SimpleXMLElement('<xml/>');

for ($i = 1; $i <= 8; ++$i) {
    $track = $xml->addChild('track');
    $track->addChild('path', "song$i.mp3");
    $track->addChild('title', "Track $i - Track Title");
}

print($xml->asXML());

?>
Ivan Krechetov
It is possible to specify the encoding with SimpleXml? How it can be done?
Eineki
+1  A: 

Since this question is tagged as homework I think you should avoid shortcuts as the templating systems and helper classes as you scope should be to learn how to do something, not get things done.

To create an XMLdocument in PHP you should instance a DOMDocument class, create child nodes and append these nodes in the correct branch of the document tree.

For reference you can read http://it.php.net/manual/en/book.dom.php

Now we will take a quick tour of the code below.

  • at line 2 we create an empty xml document (just specify xml version (1.0) and encoding (utf8))
  • now we need to populate the xml tree:
    • We have to create an xmlnode (line 5)
    • and we have to append this in the correct position. We are creating the root so we append this directly to the domdocument.
    • Note create element append the element to the node and return the node inserted, we save this reference to append the track nodes to the root node (incidentally called xml).

These are the basics, you can create and append a node in just a line (13th, for example), you can do a lot of other things with the dom api. It is up to you.

<?php    
1  /* create a dom document with encoding utf8 */
2  $domtree = new DOMDocument('1.0', 'UTF-8');

4  /* create the root element of the xml tree */
5  $xmlRoot = $domtree->createElement("xml");
6  /* append it to the document created */
7  $xmlRoot = $domtree->appendChild($xmlRoot);

9  $currentTrack = $domtree->createElement("track");
10 $currentTrack = $xmlRoot->appendChild($currentTrack);

12 /* you should enclose the following two lines in a cicle */
13 $currentTrack->appendChild($domtree->createElement('path','song1.mp3'));
14 $currentTrack->appendChild($domtree->createElement('title','title of song1.mp3'));

15 $currentTrack->appendChild($domtree->createElement('path','song2.mp3'));
16 $currentTrack->appendChild($domtree->createElement('title','title of song2.mp3'));

18 /* get the xml printed */
19 echo $domtree->saveXML();
?>

Edit: Just one other hint: The main advantage of using an xmldocument (the dom document one or the simplexml one) instead that printing the xml,is that the xmltree is searchable with xpath query

Eineki