1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
$file = "xml/phrase.xml";
//echo $file."\n";
global $inTag;
$inTag = "";
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 1);
xml_set_processing_instruction_handler($xml_parser, "pi_handler");
xml_set_default_handler($xml_parser, "parseDEFAULT");
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "contents");
if (!($fp = fopen($file, "r"))) {
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)));
}
}
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);
function startElement($parser, $name, $attrs) {
global $inTag;
global $depth;
$padTag = str_repeat(str_pad(" ", 3), $depth);
if (!($inTag == "")) {
echo ">";
}
// ouverture tag span pour word
$span ="span";
echo "\n$padTag<$span";
foreach ($attrs as $key => $value) {
echo "\n$padTag".str_pad(" ", 3);
//impression des attributs
if($key=="function"){
$key="class"; echo " $key=\"$value\"";
}
else if($key=="type"){$key="class"; $key2="title"; echo " $key=\"$value\" $key2=\"$value\"";}
else if($key=="xml:lang"){}
//else if($key=="n"){}
else {}
//echo " $key=\"$value\"";
}
$inTag = $name;
$depth++;
}
function endElement($parser, $name) {
global $depth;
global $inTag;
global $closeTag;
$depth--;
if ($closeTag == TRUE) {
// fermeture tag span pour word
$span ="span";
echo "</$span>";
$inTag = "";
} elseif ($inTag == $name) {
echo " />";
$inTag = "";
} else {
$padTag = str_repeat(str_pad(" ", 3), $depth);
//----- fermeture tag span pour phr -----//
$span ="span";
echo "\n$padTag</$span>";
}
}
function contents($parser, $data) {
global $closeTag;
$data = preg_replace("/^\s+/", "", $data);
$data = preg_replace("/\s+$/", "", $data);
if (!($data == "")) {
echo ">$data";
$closeTag = TRUE;
} else {
$closeTag = FALSE;
}
}
function parseDEFAULT($parser, $data) {
$data = preg_replace("/</", "<", $data);
$data = preg_replace("/>/", ">", $data);
echo $data;
}
function pi_handler($parser, $target, $data) {
echo "<?$target $data?>\n";
} |