List Info

Thread: note 76429 added to ref.xmlreader




note 76429 added to ref.xmlreader
user name
2007-07-14 18:19:31
I found it a little hard to parse nested elements, so wrote
a function simplifies it (based off htt
p://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  -- h
ttps://master.php.net/note/delete/76429/integrated
Del: useless     -- http
s://master.php.net/note/delete/76429/useless
Del: bad code    -- htt
ps://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     -- http
s://master.php.net/note/delete/76429/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/76429
Reject      -- https://mast
er.php.net/note/reject/76429
Search      -- https://
master.php.net/manage/user-notes.php

-- 
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php


note 76429 modified in ref.xmlreader by vrana
user name
2007-11-06 03:19:29
I found it a little hard to parse nested elements, so wrote
a function simplifies it (based off htt
p://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>

--was--
I found it a little hard to parse nested elements, so wrote
a function simplifies it (based off htt
p://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>

http://php
.net/manual/en/ref.xmlreader.php

-- 
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php


[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )