Transtypage XML -> HTML

Exemple #1 Transtypage XML -> HTML

  1. <?php
  2. $file = "data.xml";
  3. $map_array = array(
  4. "BOLD" => "B",
  5. "EMPHASIS" => "I",
  6. "LITERAL" => "TT"
  7. );
  8.  
  9. function startElement($parser, $name, $attrs)
  10. {
  11. global $map_array;
  12. if (isset($map_array[$name])) {
  13. echo "<$map_array[$name]>";
  14. }
  15. }
  16.  
  17. function endElement($parser, $name)
  18. {
  19. global $map_array;
  20. if (isset($map_array[$name])) {
  21. echo "</$map_array[$name]>";
  22. }
  23. }
  24.  
  25. function characterData($parser, $data)
  26. {
  27. echo $data;
  28. }
  29.  
  30. $xml_parser = xml_parser_create();
  31. // Utilisons la gestion de casse, de manière à être sûrs de trouver la balise dans $map_array
  32. xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
  33. xml_set_element_handler($xml_parser, "startElement", "endElement");
  34. xml_set_character_data_handler($xml_parser, "characterData");
  35. if (!($fp = fopen($file, "r"))) {
  36. die("could not open XML input");
  37. }
  38.  
  39. while ($data = fread($fp, 4096)) {
  40. if (!xml_parse($xml_parser, $data, feof($fp))) {
  41. die(sprintf("Erreur XML: %s at line %d",
  42. xml_error_string(xml_get_error_code($xml_parser)),
  43. xml_get_current_line_number($xml_parser)));
  44. }
  45. }
  46. xml_parser_free($xml_parser);
  47. ?>

LoadingChargement en cours