I'm trying to figure out how to parse very simple XML files using PHP 
4.3. I have been unsuccessful at finding simple explanations of how 
to do this. 

I found the following code somewhere (can't remember where) and it 
works great EXCEPT it does not keep my CSS class tags. The result is 
that everything shows up as a <p> rather than <p class="text">, etc. 
How do I modify this code to allow this? Or is there a better way to 
accomplish this? 

TIA

Sue

<?php
$file = "myfile.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);
?> 





Community email addresses:
  Post message: [email protected]
  Subscribe:    [EMAIL PROTECTED]
  Unsubscribe:  [EMAIL PROTECTED]
  List owner:   [EMAIL PROTECTED]

Shortcut URL to this page:
  http://groups.yahoo.com/group/php-list 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/php-list/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/php-list/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:[EMAIL PROTECTED] 
    mailto:[EMAIL PROTECTED]

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 

Reply via email to