note 37411 added to function.xml-parse
| From: | Jagan&Chaitanya at rn2 dot php dot net | Date: | Fri, 14 Nov 2003 02:54:35 +0000 |
| Subject: | note 37411 added to function.xml-parse | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-60363@lists.php.net to get a copy of this message | ||
Adam's code is great. This XML parser works with any XML document and pushes the data in an
array. This code adds in to Adam's code and helps in knowing how to extract the data from
array. This will be useful for those who are not so familiar with the object oriented PHP.
I am trying to retrieve only Address and Owner fields here
*******XML file i used:********
<?xml version="1.0" encoding="utf-8" ?>
<DATA>
<REPORT_DATE>11-Nov-2003</REPORT_DATE>
<IP_RECORD>
<ADDRESS>67.31.42.63</ADDRESS>
<HOSTNAME>dialup-67.31.42.63.Dial1.NewYork1.Level3.net</HOSTNAME>
<OWNER>Broomfield CO 80021 United States</OWNER>
</IP_RECORD>
<IP_RECORD>
<ADDRESS>152.163.253.33</ADDRESS>
<OWNER>VA 20166 United States</OWNER>
</IP_RECORD>
</DATA>
**********Code***************
<%php
// parsing the filename.xml file for IPAddress and it's owner
$filename = "filename.xml";
$xmlC = new XmlC();
$xml_data = file_get_contents( $filename );
$xmlC->Set_XML_data( $xml_data );
echo( "<pre>\n" ); // shows output in browser
print_r( $xmlC->obj_data );
echo( "</pre>\n" );
class XmlC
{
var $xml_data;
var $obj_data;
var $pointer;
function XmlC()
{
}
function Set_xml_data( &$xml_data )
{
$this->index = 0;
$this->pointer[] = &$this->obj_data;
$this->xml_data = $xml_data;
$this->xml_parser = xml_parser_create( "UTF-8" );
xml_parser_set_option( $this->xml_parser, XML_OPTION_CASE_FOLDING, false );
xml_set_object( $this->xml_parser, &$this );
xml_set_element_handler( $this->xml_parser, "_startElement",
"_endElement");
xml_set_character_data_handler( $this->xml_parser, "_cData" );
xml_parse( $this->xml_parser, $this->xml_data, true );
xml_parser_free( $this->xml_parser );
}
function _startElement( $parser, $tag, $attributeList )
{
foreach( $attributeList as $name => $value )
{
$value = $this->_cleanString( $value );
$object->$name = $value;
}
eval( "\$this->pointer[\$this->index]->" . $tag . "[] = \$object;" );
eval( "\$size = sizeof( \$this->pointer[\$this->index]->" . $tag . "
);" );
eval( "\$this->pointer[] = &\$this->pointer[\$this->index]->" . $tag .
"[\$size-1];" );
$this->index++;
}
function _endElement( $parser, $tag )
{
array_pop( $this->pointer );
$this->index--;
}
function _cData( $parser, $data )
{
if( trim( $data ) )
{
$this->pointer[$this->index] = trim( $data );
}
}
function _cleanString( $string )
{
return utf8_decode( trim( $string ) );
}
}
//retriving information stored in Address and Owner arrays
$counting=count($xmlC->obj_data->DATA[0]->IP_RECORD); //to find out the number of elements
in the array
$IPADDR="";
$OWNER="";
for($i=0;$i<$counting;++$i)
{
$IPADDR[$i]=$xmlC->obj_data->DATA[0]->IP_RECORD[$i]->ADDRESS[0];
$OWNER[$i]=$xmlC->obj_data->DATA[0]->IP_RECORD[$i]->OWNER[0];
}
echo("<center><TABLE border=1>");
for($i=0;$i<$counting;++$i)
{ echo
"<tr><td>$IPADDR[$i]</td><td>$OWNER[$i]</td></tr>"; }
echo("</TABLE></center>");
?>
----
Manual Page -- http://www.php.net/manual/en/function.xml-parse.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+37411
Delete -- http://master.php.net/manage/user-notes.php?action=delete+37411&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+37411&report=yes
Search -- http://master.php.net/manage/user-notes.php