note 76429 added to ref.xmlreader

From: Date: Sat, 14 Jul 2007 23:19:31 +0000
Subject: note 76429 added to ref.xmlreader
Groups: php.notes 
Request: Send a blank email to php-notes+get-128531@lists.php.net to get a copy of this message
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): 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> ---- Server IP: 69.147.83.197 Probable Submitter: 75.24.110.5 ---- Manual Page -- http://www.php.net/manual/en/ref.xmlreader.php Edit -- https://master.php.net/note/edit/76429 Del: integrated -- https://master.php.net/note/delete/76429/integrated Del: useless -- https://master.php.net/note/delete/76429/useless Del: bad code -- https://master.php.net/note/delete/76429/bad+code Del: spam -- https://master.php.net/note/delete/76429/spam Del: non-english -- https://master.php.net/note/delete/76429/non-english Del: in docs -- https://master.php.net/note/delete/76429/in+docs Del: other reasons-- https://master.php.net/note/delete/76429 Reject -- https://master.php.net/note/reject/76429 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#128531) next »