[ create a new paste ] login | about

Link: http://codepad.org/0faT26Di    [ raw code | output | fork ]

PHP, pasted on Oct 16:
<?php
header('Content-Type: text/plain');
$xml = <<<END
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://www.site.com/SMART/Updates">
<NewDataSet>
    <record>
        <FIELD1>data1</FIELD1>
        <FIELD2>data2</FIELD2>
        <FIELD3>data3</FIELD3>
    </record>
    <record>
        <FIELD1>data1</FIELD1>
        <FIELD2>data2</FIELD2>
        <FIELD3>data3</FIELD3>
    </record>
</NewDataSet>
</string>
END;
$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
$dom->normalize();
$dom->loadXML($xml);
echo 'Root element name: ' . $dom->firstChild->firstChild->tagName . PHP_EOL;
echo 'Number of child elements: ' . count($dom->firstChild->firstChild->childNodes) . PHP_EOL;
echo '=====' . PHP_EOL . PHP_EOL;
echo print_node($dom->firstChild->firstChild);
function print_node($node, $level = 0, $prev_level = 0) {
	$result = '';
	if($node->hasChildNodes()) {
		foreach($node->childNodes as $subnode) {
			$result .= str_repeat('    ', $level) . $node->tagName . ' =>' . PHP_EOL;
			$result .= print_node($subnode, $level + 1, $level) . PHP_EOL;
		}
	} else {
		if(trim($node->nodeValue) !== '') {
			$result .= str_repeat('    ', $level) . '**Data: ' . trim($node->nodeValue) . PHP_EOL;
		}
	}
	return $result;
}
?>


Output:
Root element name: NewDataSet
Number of child elements: 1
=====

NewDataSet =>
    record =>
        FIELD1 =>
            **Data: data1


    record =>
        FIELD2 =>
            **Data: data2


    record =>
        FIELD3 =>
            **Data: data3



NewDataSet =>
    record =>
        FIELD1 =>
            **Data: data1


    record =>
        FIELD2 =>
            **Data: data2


    record =>
        FIELD3 =>
            **Data: data3





Create a new paste based on this one


Comments: