Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

xml and php

Status
Not open for further replies.

dkin

Programmer
Dec 11, 2002
39
ES
What is the easiest way to export data to xml file using php?
 
Sure, it you use the right data-structures.

Let's say you want to export some information about library books in XML and store the information in the following structure:

Code:
$set_of_books = array
(
  [1] => array
  (
    [title] => 'PHP and MySQL for Dummies',
    [authors] => array
    (
      [name] => 'Janet Valade'
    )
    [bookinfo] => array
    (
      [binding] => 'paperback',
      [pages] => 408,
      [publisher] => 'John Wiley and Sons'
    )
  ),
  [2] => array
  (
    [title] => 'the Internet for Dummies',
    [authors] => array
    (
      [name] => 'John R. Levine',
      [name] => 'Carol Baroudi',
      [name] => 'Margaret Levine-Young'
    )
    [bookinfo] => array
    (
      [binding] => 'paperback',
      [pages] => 360,
      [publisher] => 'John Wiley and Sons'
    )
  )
)

Then it would be a fairly simple matter of recursively traversing the array and outputting keynames as XML tags.

If you take a look in the PHP online documentation, there is, in the user-supplied comments, a script snippet which talks about this. Want the best answers? Ask the best questions: TANSTAAFL!
 
There is that method, or, if you're using small XML files as I am, and you don't fancy writing an XML class and all that stuff cos it all seems so complicated to you, like me, you can simply lay out what you want to write, eg

Code:
$xmlcontents = &quot;<?xml version=\&quot;1.0\&quot;?>
<everything>
  <title>title</title>
  <author>author</author>
  <review>
blablabla
yeah yeah yeah
whatever  =)
  </review>
</everything>&quot;;

And then just open the file and write to it as normal

Code:
$fp=fopen($filename,&quot;w&quot;);
fwrite($fp,$writetofile);
fclose($fp);

Then, more importantly, to read files, use this function

Code:
function untag($string,$tag,$mode){
	$tmpval=&quot;&quot;;
	$preg=&quot;/<&quot;.$tag.&quot;>(.*?)<\/&quot;.$tag.&quot;>/si&quot;;
	preg_match_all($preg,$string,$tags);
	foreach ($tags[1] as $tmpcont){
		if ($mode==1){$tmpval[]=$tmpcont;}
		else {$tmpval.=$tmpcont;}
		}
	return $tmpval;
}

which will grab the data you want from your xml files. eg

Code:
$xmlfile = fopen($filename,&quot;r&quot;);
$xmlfilecontents = fread($xmlfile, filesize($filename));
fclose($xmlfile);
$xmldata = untag($xmlfilecontents,&quot;everything&quot;,0);
$title = untag($xmlfile,&quot;title&quot;,0);
$author = untag($xmlfile,&quot;author&quot;,0);

That would get your your items. Or you can call untag with 1 not 0 as an arguement and it will return the contents in an array.

Just thought someone might want to use this non complicated way to utilise xml files perfectly validly and fast, though everytime you want to change a value you have to read it in and thenlay it out with the new value and write it again, which is where real xml parsing things are better, along with unknown contents in your xml file, my method requires you knowing what's in there, which is no problem for small files ;) _________________________________
Leozack
Code:
MakeUniverse($infinity,1,42);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top