hi all,
here is an xml script that i came across on php.net, which i have tweaked around to do what i want. right now it outputs unto the web browser, how can i make it output to a file? or to an array? any ideas, anyone.
thanks:
the code below basically replaces any html tags in the original xml with the the word Paragraph, and then outputs to the web browser.
<?php
$file = "test.xml";
define ('LT', '<');
define ('GT', '>');
$map_array = array(
"BOLD" => "B",
"PARAGRAPH" => "P",
"EMPHASIS" => "I"
);
$movie = array();
function startElement($parser, $name, $attrs)
{
global $map_array;
if(in_array($name,$map_array))
{
$test.=str_replace("<$name>", "PARAGRAPH", "<$name>");
}
else
{
$test.= LT.strtolower($name).GT;
}
}
function endElement($parser, $name)
{
global $map_array;
if(in_array($name,$map_array))
{
print str_replace("</$name>", "PARAGRAPH", "</$name>");
}
else
{
$test.= LT."/".strtolower($name).GT;
}
}
function characterData($parser, $cdata)
{
$test.= $cdata;
}
$xml_parser = xml_parser_create();
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);
if($testFile=fopen('test.xml', "w"))
{
fwrite($testFile, $test);
}
?>