note 83812 added to class.xmlreader
| From: | EllisGL at osu1 dot php dot net | Date: | Thu, 12 Jun 2008 19:44:30 +0000 |
| Subject: | note 83812 added to class.xmlreader | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-140390@lists.php.net to get a copy of this message | ||
So I need to quickly process sections of a large XML file and I came up with this really dirty class
file.
<?php
// Quick And Dirty XML to Array V 1.00
// Quick in speed, Dirty with the 2D non assoc array it makes.
// By: EllisGL 06/12/08
//
// Usuage:
// $xml = new XML_Fast_Parse($data=string, $section=string [, $callback=string][, $type=int]);
// $xml->parseIt();
//
// $data: Either a file location or string data
// $section: the section where you want to get data from
// $callback: Name of external function - this will process the array for each section
// $type: 0 for file, 1 for string data
class XML_QND
{
private $reader = "";
private $section = "";
private $callback = "";
function XML_Fast_Parse($data, $section, $callback="", $type=0)
{
$this->reader = new XMLReader();
$this->section = $section;
$this->callback = $callback;
if($type === 0)
{
// File
$this->reader->open($data);
}
else
{
// String
$this->reader->XML($data);
}
}
function parseIt()
{
while($this->reader->read())
{
if($this->reader->nodeType == 1 &&
$this->reader->localName == $this->section)
{
do
{
// Expand the rest of the data to text
$element = $this->reader->expand();
// Convert to an array
$arr = explode("\n", $element->textContent);
// Clean up the array...
foreach($arr as $key=>$val)
{
$arr[$key] = ltrim($val);
}
// Call back
$call = $this->callback;
$call($arr);
}while($this->reader->next('item_data'));
break;
}
}
return($arr);
}
}
set_time_limit(0);
function echoit($arr)
{
echo '<pre>';
print_r($arr);
echo '</pre>';
}
$data ='<?xml version="1.0" encoding="UTF-8" ?>
<DataFeeds>
<item_data>
<item_basic_data>
<item_unique_id>aaa</item_unique_id>
<item_sku>bbb</item_sku>
<item_upc>ccc</item_upc>
<item_mpn>ddd</item_mpn>
<item_brand>eee</item_brand>
</item_basic_data>
<merch_cat_list>
<merch_cat_item>
<merch_cat_name>fff</merch_cat_name>
</merch_cat_item>
<merch_cat_item>
<merch_cat_name>gggg</merch_cat_name>
</merch_cat_item>
</merch_cat_list>
</item_data>
<item_basic_data>
<item_unique_id>aaa</item_unique_id>
<item_sku>bbb</item_sku>
<item_upc>ccc</item_upc>
<item_mpn>ddd</item_mpn>
<item_brand>eee</item_brand>
</item_basic_data>
<merch_cat_list>
<merch_cat_item>
<merch_cat_name>fff</merch_cat_name>
</merch_cat_item>
<merch_cat_item>
<merch_cat_name>gggg</merch_cat_name>
</merch_cat_item>
</merch_cat_list>
</item_data>
</DataFeeds>';
$xml = new XML_QND($data, 'item_data', 'echoit', 1);
$xml->parseIt();
?>
----
Server IP: 64.71.164.2
Probable Submitter: 71.123.214.164
----
Manual Page -- http://www.php.net/manual/en/class.xmlreader.php
Edit -- https://master.php.net/note/edit/83812
Del: integrated -- https://master.php.net/note/delete/83812/integrated
Del: useless -- https://master.php.net/note/delete/83812/useless
Del: bad code -- https://master.php.net/note/delete/83812/bad+code
Del: spam -- https://master.php.net/note/delete/83812/spam
Del: non-english -- https://master.php.net/note/delete/83812/non-english
Del: in docs -- https://master.php.net/note/delete/83812/in+docs
Del: other reasons-- https://master.php.net/note/delete/83812
Reject -- https://master.php.net/note/reject/83812
Search -- https://master.php.net/manage/user-notes.php