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
| <?php
$fichier = 'opcvm_all.xml';
$parseur = xml_parser_create();
if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['id'])) {
$display = FALSE;
function startDocument() {}
function endDocument() {}
function startElement($parser, $name, $attrs) {
if ($GLOBALS['display']) {
echo "<$name>";
}
if (!strcasecmp($name, 'opcvm') && $attrs['ID'] == $_POST['id']) {
$GLOBALS['display'] = TRUE;
}
}
function endElement($parser, $name) {
if ($GLOBALS['display']) {
if (!strcasecmp($name, 'opcvm')) {
$GLOBALS['display'] = FALSE;
} else {
echo "</$name><br/>";
}
}
}
function characterData($parseur, $data) {
if ($GLOBALS['display']) {
echo $data;
}
}
xml_set_element_handler($parseur, 'startElement', 'endElement');
xml_set_character_data_handler($parseur, 'characterData');
} else {
function startDocument() {
echo '<form method="post">';
echo 'Id : <select name="id">';
}
function endDocument() {
echo '</select>';
echo '<br />';
echo '<input type="submit" value="Valider" />';
echo '</form>';
}
function startElement($parser, $name, $attrs) {
if (!strcasecmp($name, 'opcvm') && isset($attrs['ID'])) {
echo '<option>' . $attrs['ID'] . '</option>';
}
}
function endElement($parser, $name) {}
xml_set_element_handler($parseur, 'startElement', 'endElement'/*FALSE*/);
}
$fp = fopen($fichier, 'r');
if (!$fp) die("Impossible d'ouvrir le fichier XML");
startDocument();
while ($ligneXML = fgets($fp, 1024)) {
xml_parse($parseur, $ligneXML, feof($fp)) or die(sprintf("Erreur XML : %s à la ligne %d", xml_error_string(xml_get_error_code($parseur)), xml_get_current_line_number($parseur)));
}
endDocument();
xml_parser_free($parseur);
fclose($fp); |
Partager