downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  

<ExamplesXML External Entity Example>
Last updated: Thu, 26 Jun 2008

XML Tag Mapping Example

Example #1 Map XML to HTML

This example maps tags in an XML document directly to HTML tags. Elements not found in the "map array" are ignored. Of course, this example will only work with a specific XML document type.

<?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);
?>



add a noteadd a note User Contributed Notes
XML Tag Mapping Example
There are no user contributed notes for this page.




<ExamplesXML External Entity Example>
Last updated: Thu, 26 Jun 2008
show source | credits | sitemap | contact | advertising | mirror sites
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: http://phpbuilder.com/
Last updated: Tue Nov 1 20:20:59 2005 EST
Columns / Articles | Tips / Quickies | News | News Linking and RSS Feeds | Shared Code Library
Mail Archives | Support / Discussion Forums | Get Started! Links | Contribute! | Docs