xmlTree

minimalistic XML processore for PHP
xmlTree is a tiny, little XML processor for PHP that doesn't even offer 10% of what the XML spec has to offer, but still manages to do almost everything a normal developer needs (and besides, it's easy to extend). It's meant for all those people that So, it's really quite minimalistic. It was initially written to store configuration data for another program, but ended but managing quite a bit of the HTML code too.
Essentially, it provides the following features: And that's pretty much all there is to it. the code can be found here and here. The code that generates the page you're currently viewing is included below. The only things that I think might need some explanation are the XMLElement constructor and the result returned by XMLParser->getTree: One more thing: There's some stuff that prevents sucessful parsing, so make sure to avoid it: A last note: I do not intend to maintain this code, it's really just released as it is, take it or leave it. The license is GPL and I take no warranty whatsoever, unless required by law.
<?

require_once("xmlTree.php");
	
$progName="xmlTree";
$progSubName="minimalistic XML processore for PHP";


$document=new XMLDocument($progName." - ".$progSubName);
$bodys=$document->getElementsByTagName("body");
$body=$bodys[0];

$bodyContentParser=new XMLParser("
	<div>
		<h1 id='topheader'>$progName</h1>
		<h5 id='subheader'>$progSubName</h5>
		<div>
			<div id='adContainer'></div>
			xmlTree is a tiny, little XML processor for PHP that doesn't even offer 10% of what the XML spec has to offer, but still manages to
			do almost everything a normal developer needs (and besides, it's easy to extend). It's meant for all those people that
			<ul>
				<li>Hate to use code that they cannot possibly understand</li>
				<li>Prefer small libraries</li>
				<li>Don't expect the XML Library to validate their data</li>
				<li>Don't need the XML library to handle data that's not trustworthy</li>
				<li>Don't need automatic character conversion, like \" to \\\" or < to &lt;</li>
			</ul>
			So, it's really quite minimalistic. It was initially written to store configuration data for
			another program, but ended but managing quite a bit of the HTML code too.<br />
			Essentially, it provides the following features:
			<ul>
				<li>An XMLElement class that provides parentNode, childNodes, tagName and attributes. It also provides a value for text nodes</li>
				<li>A Javascript like DOM manipulation system, featuring such gems as appendChild, setAttribute,
					getAttribute, removeChild, getElementsByTagName, getElementsByName,getElementById, toXML and toFile, all of which
					(with the exception of the last two methods) behave almost exactly like their Javascript counterparts.</li>
				<li>An XMLDocument class with html, head, title and body (nothing special, but it makes life easier)</li>
				<li>An XMLParser class that turns an XML string into a XMLElement tree.</li>
			</ul>
			And that's pretty much all there is to it. the code can be found <a href='xmlTree.php.txt'>here</a> and <a href='fileStreams.php.txt'>here</a>.
			The code that generates the page you're currently viewing is included below. The only things that I think might need some explanation are the XMLElement constructor and the result returned by XMLParser->getTree:
			<ul>
				<li>The XMLElement constructor takes the tag name as the first parameter. If FALSE is passed as the first parameter, along with a string as second parameter, the XMLElement becomes a text node.</li>
				<li>XMLParser->getTree returns the first top level element found. If the string that you were parsing contained more than one
					top level element you can get them through XMLParser->topElement->childNodes</li>
			</ul>
			One more thing: There's some stuff that prevents sucessful parsing, so make sure to avoid it:
			<ul>
				<li>Escaped quotes in attributes. The parser simply ignores it, so alt=" Hello, \\"Hans\\" " fails. Use different quotes instead ( alt=' Hello, "Hans" ' )</li>
				<li>Literal < and >. Make sure you convert those to &lt; and &gt; otherwise the parser will mistake them for tag starts and ends.</li>
			</ul>
			A last note: I do not intend to maintain this code, it's really just released as it is, take it or leave it. The license is GPL and I take no warranty whatsoever, unless required by law.
			<pre id='codeSample'></pre>
		</div>
	</div>
");


$bodyContentTree=$bodyContentParser->getTree();
$body->appendChild($bodyContentTree);

$body->setAttribute("style","font-family:Verdana;");
$document->getElementById("topheader")->setAttribute("style","font-family:Courier New; margin-bottom:3px;");
$document->getElementById("subheader")->setAttribute("style","margin-top:0px; padding-top:0px;");

$style=new XMLElement("style");
$style->setAttribute("type","text/css");
$style->appendChild(new XMLElement(FALSE,"body{ font-size:12px; } #adContainer {margin-bottom:20px; margin-left:120px;}"));

$ad1=new XMLElement("script");
$document->getElementById("adContainer")->appendChild($ad1);
$ad1->setAttribute("type","text/javascript");
$ad1->appendChild(new XMLElement(FALSE,'<!--
			google_ad_client = "pub-6117614560058507";
			google_alternate_color = "FFFFFF";
			google_ad_width = 728;
			google_ad_height = 90;
			google_ad_format = "728x90_as";
			google_ad_type = "text";
			google_ad_channel = "";
			//-->'));

			
$ad2=new XMLElement("script");
$document->getElementById("adContainer")->appendChild($ad2);
$ad2->setAttribute("type","text/javascript");
$ad2->setAttribute("src","http://pagead2.googlesyndication.com/pagead/show_ads.js");
$ad2->appendChild(new XMLElement(FALSE,' '));




$heads=$document->getElementsByTagName("head");
$head=$heads[0];
$head->appendChild($style);

$contentReader=new readableFile("xmlSample.php");

$codeSampleBlock=$document->getElementById("codeSample");
$codeSampleBlockContent=$contentReader->read();
$codeSampleBlockContent=str_replace ( "<", "&"."lt;", $codeSampleBlockContent);
$codeSampleBlockContent=str_replace ( ">", "&"."gt;", $codeSampleBlockContent);

$codeSampleBlock->appendChild(new XMLElement(FALSE,$codeSampleBlockContent));


print($document->toXML());
?>