Jump to content

Best Way To Build And Write XML Files


JustinK101

Recommended Posts

I need to build XML files dynamically and then write them to the web server disk.

 

What is the best way of doing this? Is it simply the best to build up a string internally in php and then write the string to a file on the web server, or should I use an XML builder class such as simpleXML?

 

Here is an example of what the XML files I need to build is like:

 

<graph>
  <general_settings bg_color="ffffff" showAnchor="1" showArea="0" type_animation="2">
    <header text="Total points" font="Verdana" color="000000" size="18"/>
    <subheader text="" font="Verdana" color="000000" size="14"/>
    <legend font="Verdana" color="000000" font_size="11"/>
    <legend_popup font="Verdana" bgcolor="FFFFE3" font_size="10"/>
    <Xheaders rotate="0" color="000000" size="10" title="" title_color="000000"/>
    <Yheaders color="000000" size="10" title="" title_rotate="90" title_color="000000"/>
    <grid showX="1" showY="1" persent_stepY_from_stepX="120" grid_width="550" grid_height="250" grid_color="000000" grid_alpha="20" grid_thickness="1" bg_color="ffffff" bg_alpha="100" alternate_bg_color="ffffff" border_color="000000" border_thickness="2"/>
  </general_settings>
  <abscissa_data>
    <x value="1990"/>
    <x value="1992"/>
    <x value="1994"/>
    <x value="1996"/>
    <x value="1998"/>
    <x value="2000"/>
    <x value="2001"/>
    <x value="2002"/>
    <x value="2003"/>
  </abscissa_data>
  <ordinate_data seriesName="ελληνική γλώσσα" color="0080C0" size="3.5">
    <y value="1358"/>
    <y value="1482"/>
    <y value="1666"/>
    <y value="1811"/>
    <y value="2051"/>
    <y value="2138"/>
    <y value="2209"/>
    <y value="2247"/>
    <y value="2301"/>
  </ordinate_data>
  <ordinate_data seriesName="български език" color="FF0000" size="3.5">
    <y value="1125"/>
    <y value="1320"/>
    <y value="1518"/>
    <y value="1797"/>
    <y value="1893"/>
    <y value="2010"/>
    <y value="2057"/>
    <y value="2166"/>
    <y value="2197"/>
  </ordinate_data>
  <ordinate_data seriesName="русский язык" color="FF0080" size="3.5">
    <y value="2264"/>
    <y value="2282"/>
    <y value="2365"/>
    <y value="2433"/>
    <y value="2559"/>
    <y value="2823"/>
    <y value="2867"/>
    <y value="2867"/>
    <y value="2867"/>
  </ordinate_data>
</graph>

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/99272-best-way-to-build-and-write-xml-files/
Share on other sites

No one on these forums seems to know anything about XML! You need to use the xmlWriter class. This is the way I got it to take a form and make it into a webpage. The only difference is you need to use the save function. It would be like save("filename.xml") I believe. Right now this will take the form:

 

	<form method="post" action="newpage_submit.php">
	<input type="text" value="Title" name="title"/><br />
	<input type="text" value="Sub Header" name="subheader" /><br />
	<textarea cols="50" rows="4" name="content"></textarea><br />
	<input type="submit" name="save" />
</form>

 

 

 

And then here is the post page:

<?php 
        $newpage_title = $_POST['title'];
$newpage_subheader = $_POST['subheader'];
$newpage_content = $_POST['content'];
        $newpage_content = str_replace("\r", "<br />", $newpage_content); //Grabs the returns in the textarea box above and makes them into br tags. They could be p tags as well.

    // Code below would be in the body
    $xw = new xmlWriter();
    $xw->openMemory();
    $xw->setIndent(true); //Makes the code look nice
    $xw->startElement('webpage'); //Create my root tag
    	$xw->writeElement ('title', $newpage_title); //The title is what the tag is and the the second part will put the actual content in.
	$xw->writeElement('subhead', $newpage_subheader);
	$xw->writeElement('content', $newpage_content);
    $xw->endElement(); // closes the webpage node
$xw = $xw->outputMemory(true); //False if you dont want it to display
$xw = str_replace('&#13;', '', $xw); //NOTE: xmlWriter will convert special characters to ASCII. Here I take the ASCII characters and then replace them with the actual special characters which will make it display the < > and "
$xw = str_replace('<', '<', $xw);
$xw = str_replace('>', '>', $xw);

?>

 

 

Hope this helps a little. That will actually make a webpage, with how I have my XML parser set up. Here is that code in case you are interested.

 

<?php

//Gets title of page
define('PAGE_TITLE', ($webpage->title));

//Gets title of page for the content
define('TITLE', '<h1>'.($webpage->title).'</h1>');

//Gets sub header of page
define('SUBHEAD', '<h2>'.($webpage->subhead).'</h2>');

//Parses everything into HTML
function page_content() {
	echo '<div id="mainContent">';
	echo TITLE;
	echo SUBHEAD;
	global $webpage;
	foreach($webpage->content->p as $p) {
		echo '<p>'.($p).'</p>';
	}
	echo'</div>';	
}

?>

 

That way you add this function into a template and by changing the page id tag which is on every new page you create it will grab that page id from the XML array. The only problem and it's a big one that is preventing me from finishing this XML CMS tool is i don't know how to write to a preexisting xml document. If you know how let me know!

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.