2
$xml = '<?xml version="1.0" encoding="utf-8" ?>';
4
foreach ($results as $k => $entity) {
5
$xml .= '<entity type="'.$type.'">';
6
foreach ($entity as $key => $value) {
7
$xml .= '<'.(is_numeric($key)?'element id="'.$key.'"':$key).'>'.dump($value).'</'.(is_numeric($key)?'element':$key).'>';
15
if (is_null($value)) {
20
if (is_array($value)) {
21
foreach ($value as $k => $v) {
22
$response .= "<{$k}>".dump($v)."</{$k}>";
25
if (is_object($value)) {
26
$response .= dump($value->getRawValue());
35
* Receives a flat XML string and make it well-formatted
36
* @author http://recursive-design.com/blog/2007/04/05/format-xml-with-php/
37
* @param string $xml The original XML string to process
38
* @return string Well-formatted version of the original XML string
40
function formatXmlString($xml)
42
// add marker linefeeds to aid the pretty-tokeniser (adds a linefeed between all tag-end boundaries)
43
$xml = preg_replace('/(>)(<)(\/*)/', "$1\n$2$3", $xml);
45
// now indent the tags
46
$token = strtok($xml, "\n");
47
$result = ''; // holds formatted version as it is built
48
$pad = 0; // initial indent
49
$matches = array(); // returns from preg_matches()
51
// scan each line and adjust indent based on opening/closing tags
52
while ($token !== false) {
53
// test for the various tag states
55
// 1. open and closing tags on same line - no change
56
if (preg_match('/.+<\/\w[^>]*>$/', $token, $matches)) {
58
// 2. closing tag - outdent now
59
} elseif (preg_match('/^<\/\w/', $token, $matches)) {
62
// 3. opening tag - don't pad this one, only subsequent tags
63
} elseif (preg_match('/^<\w[^>]*[^\/]>.*$/', $token, $matches)) {
65
// 4. no indentation needed
70
// pad the line with the required number of leading spaces
71
$line = str_pad($token, strlen($token)+$pad, ' ', STR_PAD_LEFT);
72
$result .= $line . "\n"; // add to the cumulative result, with linefeed
73
$token = strtok("\n"); // get the next token
74
$pad += $indent; // update the pad size for subsequent lines
80
echo formatXmlString($xml);
b'\\ No newline at end of file'
1
<? echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"?>
2
<results><?php foreach ($results as $k => $entity): ?>
4
<entity type="<?php echo $type?>"><?php foreach ($entity as $key => $value): if (is_object($value)): foreach ($value as $v): ?>
6
<<?php echo $key ?>><?php echo $v ?></<?php echo $key ?>><?php endforeach; ?><?php else: ?>
8
<<?php echo $key ?>><?php echo $value?></<?php echo $key ?>><?php endif; ?><?php endforeach; ?>
10
</entity><?php endforeach; ?>
b'\\ No newline at end of file'