Starbeamrainbowlabs

simplexmlwriter.php - A string based XML writer

What it is and what it does

simplexmlwriter.php is an easy to use XMl generation class written in PHP. It simplifies the generation of XML documents by allowing XML to be generated through a series of function calls, tidying up your code and improving readability.

Example

add_xslt("https://example.com/test.xslt");	//add an xslt stylesheet
	$xml->open("root"); //open the root tag (although you could call it anything)
	
	//loop 10 times
	foreach(range(1, 10) as $i)
	{
		$xml->open("number"); //open a  tag
			$xml->addtag("type", ["foo" => "bar"], "integer"); //open and close a tag, adding text and attributes in one go
			$xml->open("value"); //open a  tag
				$xml->addtext($i); //add some text
			$xml->close(); //close the  tag
		$xml->close(); //close the  tag
	}
	$xml->closeall(); //close any other tags that are still open
	
	//------------------
	
	header("content-type: application/xml"); //set the appropriate header
	echo($xml->render()); //send the XML to the requester
?>

Reference

Name Type Description
xml_doctype constant The XML doctype that will be inserted at the top of the document
generator_info constant A string that will be inserted just below the doctype in a comment detailsing the generator that was used.
prettyprint property Whether the XML should be formatted (prettyprinted) or not. remember to set this *before* you start generating any XML as this generator is string based, and will not go back and change anything once you have changed it. Defaults to true.
add_xslt($url = "") function Add an XSLT stylesheet to the document. remember to call this before opening the first tag.
open($tagname = "root", $attributes = []) function Open a tag with the given name with the given attribute. The attributes should be an array in the from ["name1" => "value1", "name2" => "value2"].
addtext($text = "", $newline = true) function Add some text to the xml document. the $newline argument indicated whether a newline character should be added after the text.
close() function Close the last tag that was opened.
closeall() function Close all opened tags.
addtag($tagname = "root", $attributes = [], $text = "") function Add a tag with the name $tagname and then close it again. Attributes can be specified through an array (see the open() function above for detail on how to specify attributes), and text can be added through the $text argument. Pass an empty array to skip adding attributes.
addcomment($text = "") function Add a comment containing the given text.
render() function Render the xml and return the output. All open tags are closed in the rendering, but are left open in the main document so you can continue to add to the xml document after rendering.
__toString() function Alias to render(). Allows you to pass the document itself to functions, like this: echo($myxml); instead of echo($myxml->render())

Download

Simply download simplexmlwriter.php and require() it, and you are ready to use it.

Download


Art by Mythdael