Converting Sample XML to PHP DOMDocument Code

It’s time for you to build an integration and the integration partner uses a large XML document to accept data from you. They have provided a sample of the XML but no pre-built PHP code. Use this XML to PHP DOMDocument code generator to build the PHP for you.

<?php

$xmlsamplestring = <<<XML
<SampleRequest>
	<RequestDatasets>
		<RequestData>
			<Name>
				Test
			</Name>
			<Id>
				123
			</Id>
		</RequestData>
		<RequestData>
			<Name>
				Test2
			</Name>
			<Id>
				1234
			</Id>
		</RequestData>
	</RequestDatasets>
</SampleRequest>
XML;

$xmldata = simplexml_load_string($xmlsamplestring);
echo '<pre>'.make_xml_code($xmldata).'</pre>';


		function make_xml_code($xmldata,$parentnodename='$xml',$tabs='',$breaktype="<br>",$tabtype='&#9;'){
			$start_tabs = $tabs;
			$tabs = $tabs.$tabtype;
			$codetext = '';
			$subnodes = false;
			
			if($parentnodename == '$xml'){
				$codetext = '$xml = new DOMDocument(\'1.0\',\'UTF-8\');'.$breaktype;
			}
			
			foreach($xmldata as $xmlnodename=>$xmlnode){
				$attcodetext = '';
				$nodeval = '';
				$nestedcodetext = '';
				
				if($xmlnode->attributes()){
					foreach($xmlnode->attributes() as $attname=>$attvalue){
						$attcodetext .= $tabs.'$xml_'.$xmlnodename.'->setAttribute(\''.$attname.'\',\''.$attvalue.'\');'.$breaktype;
					}
				}

				if(trim($xmlnode->__toString())){
					$nodeval = ',\''.trim($xmlnode->__toString()).'\'';
				}elseif(count($xmlnode->children()) == 0){
					//empty child element
				}else{
					$nestedcodetext .= make_xml_code($xmlnode,'$xml_'.$xmlnodename,$tabs,$breaktype,$tabtype);
				}
				$codetext .= $tabs.'$xml_'.$xmlnodename.' = $xml->createElement(\''.$xmlnodename.'\''.$nodeval.');'.$breaktype;
				$codetext .= $attcodetext;
				$codetext .= $nestedcodetext;
				
				$codetext .= $start_tabs.$parentnodename.'->appendChild($xml_'.$xmlnodename.');'.$breaktype;
			}
			return $codetext;
		}

The sample XML begins on line 3 but the important part of this is the function make_xml_code(). Notice that it is a recursive function, which allows it to build XML trees to any depth. The first parameter is the actual XML being passed in as the result of simplexml_load_string() being run on line 26. The next two parameters should be left alone as they are used in the recursion. Finally the last two parameters may be set to change the character used in a line break or a tab. The results are indented using the tab for better readability.

See the results of this particular sample XML.

$xml = new DOMDocument('1.0','UTF-8');
	$xml_RequestDatasets = $xml->createElement('RequestDatasets');
		$xml_RequestData = $xml->createElement('RequestData');
			$xml_Name = $xml->createElement('Name','Test');
		$xml_RequestData->appendChild($xml_Name);
			$xml_Id = $xml->createElement('Id','123');
		$xml_RequestData->appendChild($xml_Id);
	$xml_RequestDatasets->appendChild($xml_RequestData);
		$xml_RequestData = $xml->createElement('RequestData');
			$xml_Name = $xml->createElement('Name','Test2');
		$xml_RequestData->appendChild($xml_Name);
			$xml_Id = $xml->createElement('Id','1234');
		$xml_RequestData->appendChild($xml_Id);
	$xml_RequestDatasets->appendChild($xml_RequestData);
$xml->appendChild($xml_RequestDatasets);

This gives you some code to generate the DOMDocument in PHP. Notice the values from the sample on lines 4, 6, 10, and 12 should be replaced with the data that you intend to output. Once you place that PHP into your integration you can use $xml->saveXML() to get the raw XML output as a string.

Leave a Reply

Your email address will not be published. Required fields are marked *