note 76429 deleted from class.xmlreader by thiago
| From: | thiago@php.net | Date: | Wed, 12 Nov 2008 13:44:31 +0000 |
| Subject: | note 76429 deleted from class.xmlreader by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-146436@lists.php.net to get a copy of this message | ||
Note Submitter: usedsk at aoeu dot zzn dot com
----
I found it a little hard to parse nested elements, so wrote a function simplifies it (based off http://www.thescripts.com/forum/thread627281.html):
<?php
function read_mixed_xml($filename, $arrayBeginElem, $arrayEndElem)
{
$output = "";
$arrayBeginKeys = array_keys($arrayBeginElem);
$lengthBegin = count($arrayBeginElem); // Length of the begin array
$arrayEndKeys = array_keys($arrayEndElem);
$lengthEnd = count($arrayEndElem); // Length of end element array
$xmlReader = new XMLReader();
$xmlReader->open($filename);
$xmlReader->read(); // Skip root node
/* Go through the nodes */
while($xmlReader->read())
{
/* We're only parsing begin and #text nodes right now */
if($xmlReader->nodeType != XMLReader::END_ELEMENT)
{
switch($xmlReader->nodeType)
{
/* If the current node is a begin element, go through the array of begin elements, in search of
the current node's name. If it is, append $arrayBeginElem's value for the current
node's name to the $output. (Simulates case "paragraph":
$output .= "<p>"
break;
) */
case XMLReader::ELEMENT:
for($i = 0; $i < $lengthBegin; $i++)
{
$key = $arrayBeginKeys[i];
if($key==$xmlReader->name)
{
$output .= $arrayBeginElem[$key];
}
}
break;
/* If the current node is a #text node, append the node's value to $output */
case XMLReader::TEXT:
$output .= $xmlReader->value;
break;
}
}
/* If the current node is an end element, go through the array of end elements, and search for the
current node's name. If found, append $arrayEndElem's value for the current node's
name to the output */
else if($xmlReader->nodeType == XMLReader::END_ELEMENT)
{
for($i = 0; $i < $lengthEnd; $i++)
{
$key = $arrayEndKeys[i];
if($key==$xmlReader->name)
{
$output .= $arrayEndElem[$key];
}
}
}
}
$xmlReader->close();
return $output;
}
Example input:
$begin = array("title" => " <h1>", "paragraph" => "
<p>", "italicized" => "<i>");
$end = array("title" => "</h1>", "paragraph" =>
"</p>", "italicized" => "</i>");
$content = read_mixed_xml("index.xml", $begin, $end);
echo $content;
?>
index.xml:
<?xml version="1.0"?>
<body>
<title>Introduction</title>
<paragraph>
Lorem <italicized>ipsum dolor sit amet</italicized>, consectetuer adipiscing elit. Donec
neque augue, nonummy sit amet, interdum vitae, egestas a, nulla. Aenean sed turpis eget lacus
venenatis tincidunt. Integer in leo vitae est euismod congue. Curabitur quis tellus ut nulla
pharetra fringilla. Phasellus id risus sagittis turpis lobortis pretium.
</paragraph>
<paragraph>
Curabitur ultrices pulvinar massa. Nullam ac massa. Morbi adipiscing pharetra est. In non neque
vitae massa adipiscing vestibulum. Integer congue, lacus non sagittis consectetuer, magna nisl
eleifend nisl, id fringilla justo justo et arcu.
</paragraph>
</body>
Example output:
<h1>Introduction</h1> <p>
Lorem <i>ipsum dolor sit amet</i>, consectetuer adipiscing elit. Donec neque augue,
nonummy sit amet, interdum vitae, egestas a, nulla. Aenean sed turpis eget lacus venenatis
tincidunt. Integer in leo vitae est euismod congue. Curabitur quis tellus ut nulla pharetra
fringilla. Phasellus id risus sagittis turpis lobortis pretium.
</p> <p>
Curabitur ultrices pulvinar massa. Nullam ac massa. Morbi adipiscing pharetra est. In non neque
vitae massa adipiscing vestibulum. Integer congue, lacus non sagittis consectetuer, magna nisl
eleifend nisl, id fringilla justo justo et arcu.
</p>