Page 1 of 1
XML parser
Posted: Mon Aug 06, 2007 11:27 am
by smilesmita
hi there,
I have a very complex xml doc which if convert into array gives you a lot of nested array.
i wanted some good parser which would parse this complex xml data.
any idea about a good parser or code..which would work on php 4.4.4
I want to extract values from one offset tag and get those extracted values to the DB.
please help.
Posted: Mon Aug 06, 2007 8:46 pm
by smudge
Did you try Google or
the manual?
I'm sure you could find something there...

Re: XML parser
Posted: Mon Aug 06, 2007 9:04 pm
by superdezign
smilesmita wrote:I have a very complex xml doc which if convert into array gives you a lot of nested array.
Well, what'd you expect?
http://php.net/dom
Posted: Tue Aug 07, 2007 5:43 am
by dethron
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Code: Select all
<?php
$file = "data.xml";
$map_array = array(
"BOLD" => "B",
"EMPHASIS" => "I",
"LITERAL" => "TT"
);
function startElement($parser, $name, $attrs)
{
global $map_array;
if (isset($map_array[$name])) {
echo "<$map_array[$name]>";
}
}
function endElement($parser, $name)
{
global $map_array;
if (isset($map_array[$name])) {
echo "</$map_array[$name]>";
}
}
function characterData($parser, $data)
{
echo $data;
}
$xml_parser = xml_parser_create();
// use case-folding so we are sure to find the tag in $map_array
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
?>
you should read the documentation :
http://tr2.php.net/xml
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Tue Aug 07, 2007 7:36 am
by dethron