utrecht
18-10-2004, 07:15
Tratto direttamente dal manuale, sto provando a far interagire php con xml. Ecco i primi dubbi........
Domanda no.1:
nell'esempio che seguirà (stampo a video tutto il codice xml e in grassetto i dati) non riesco a capire con quale criterio viene eseguita la stampa di $data.... andando a vedere il sorgente della pagina scopro che ci sono una serie di '<b> </b>' anche laddove non ci sono valori; forse che il parser va comunque a stampare ogni volta che incontra uno 'startElement' oppure un 'endElement'?
OK :( forse ho fatto un po' di casino nell'esporre la domanda comuque ditemi se avete capito la mia spiegazione delirante e se magari riuscite a darmi un chiarimento.
Domanda no.2:
a questo punto sorgerebbe anche il problema se eseguissi una query (supponiamo un INSERT INTO) per ogni valore incontrato perchè verrebbero inseriti valori vuoti.
Sto dicendo una riga di ca22ate oppure sono domande pertinenti?
Grazie a tutti!!!
Di seguito i codici delle pagine MA NON spaventatevi: sono di immediata lettura per voi che ne capite! :)
--- cv.php3 ---
<?php
$file = "cv.xml";
function startElement($parser, $name, $attribs)
{
echo "<<font color=\"#0000cc\">$name</font>";
if (sizeof($attribs)) {
while (list($k, $v) = each($attribs)) {
echo " <font color=\"#009900\">$k</font>=\"<font
color=\"#990000\">$v</font>\"";
}
}
echo ">";
}
function endElement($parser, $name)
{
echo "</<font color=\"#0000cc\">$name</font>>";
}
function characterData($parser, $data)
{
echo "<b>$data</b>";
}
function defaultHandler($parser, $data)
{
if (substr($data, 0, 1) == "&" && substr($data, -1, 1) == ";") {
printf('<font color="#aa00aa">%s</font>',
htmlspecialchars($data));
} else {
printf('<font size="-1">%s</font>',
htmlspecialchars($data));
}
}
function new_xml_parser($file)
{
global $parser_file;
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 1);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
xml_set_default_handler($xml_parser, "defaultHandler");
if (!($fp = @fopen($file, "r"))) {
return false;
}
if (!is_array($parser_file)) {
settype($parser_file, "array");
}
$parser_file[$xml_parser] = $file;
return array($xml_parser, $fp);
}
if (!(list($xml_parser, $fp) = new_xml_parser($file))) {
die("Non si riesce ad aprire il documento XML");
}
echo "<pre>";
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("Errore XML: %s alla linea %d\n",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
echo "</pre>";
echo "parsing completato\n";
xml_parser_free($xml_parser);
?>
--- cv.xml ---
<?xml version="1.0" standalone="no"?>
<?xml:stylesheet type="text/xsl" href="cv.xsl" ?>
<!DOCTYPE radice SYSTEM "cv.dtd">
<radice>
<curriculum>
<nome>mario</nome>
<cognome>rossi</cognome>
</curriculum>
</radice>
---cv.xsl ---
<?xml version="1.0"?>
<HTML xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<BODY STYLE="font-family:Arial, helvetica, sans-serif; font-size:22pt">
<xsl:for-each select="radice/curriculum">
<DIV STYLE="background-color:teal; color:white; margin-bottom:0.5em">
<xsl:value-of select="nome"/>
<xsl:value-of select="cognome"/>
</DIV>
</xsl:for-each>
</BODY>
</HTML>
--- cv.dtd ---
<!ELEMENT radice (curriculum+)>
<!ELEMENT curriculum (nome+,cognome)>
<!ELEMENT nome (#PCDATA)>
<!ELEMENT cognome (#PCDATA)>
Domanda no.1:
nell'esempio che seguirà (stampo a video tutto il codice xml e in grassetto i dati) non riesco a capire con quale criterio viene eseguita la stampa di $data.... andando a vedere il sorgente della pagina scopro che ci sono una serie di '<b> </b>' anche laddove non ci sono valori; forse che il parser va comunque a stampare ogni volta che incontra uno 'startElement' oppure un 'endElement'?
OK :( forse ho fatto un po' di casino nell'esporre la domanda comuque ditemi se avete capito la mia spiegazione delirante e se magari riuscite a darmi un chiarimento.
Domanda no.2:
a questo punto sorgerebbe anche il problema se eseguissi una query (supponiamo un INSERT INTO) per ogni valore incontrato perchè verrebbero inseriti valori vuoti.
Sto dicendo una riga di ca22ate oppure sono domande pertinenti?
Grazie a tutti!!!
Di seguito i codici delle pagine MA NON spaventatevi: sono di immediata lettura per voi che ne capite! :)
--- cv.php3 ---
<?php
$file = "cv.xml";
function startElement($parser, $name, $attribs)
{
echo "<<font color=\"#0000cc\">$name</font>";
if (sizeof($attribs)) {
while (list($k, $v) = each($attribs)) {
echo " <font color=\"#009900\">$k</font>=\"<font
color=\"#990000\">$v</font>\"";
}
}
echo ">";
}
function endElement($parser, $name)
{
echo "</<font color=\"#0000cc\">$name</font>>";
}
function characterData($parser, $data)
{
echo "<b>$data</b>";
}
function defaultHandler($parser, $data)
{
if (substr($data, 0, 1) == "&" && substr($data, -1, 1) == ";") {
printf('<font color="#aa00aa">%s</font>',
htmlspecialchars($data));
} else {
printf('<font size="-1">%s</font>',
htmlspecialchars($data));
}
}
function new_xml_parser($file)
{
global $parser_file;
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 1);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
xml_set_default_handler($xml_parser, "defaultHandler");
if (!($fp = @fopen($file, "r"))) {
return false;
}
if (!is_array($parser_file)) {
settype($parser_file, "array");
}
$parser_file[$xml_parser] = $file;
return array($xml_parser, $fp);
}
if (!(list($xml_parser, $fp) = new_xml_parser($file))) {
die("Non si riesce ad aprire il documento XML");
}
echo "<pre>";
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("Errore XML: %s alla linea %d\n",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
echo "</pre>";
echo "parsing completato\n";
xml_parser_free($xml_parser);
?>
--- cv.xml ---
<?xml version="1.0" standalone="no"?>
<?xml:stylesheet type="text/xsl" href="cv.xsl" ?>
<!DOCTYPE radice SYSTEM "cv.dtd">
<radice>
<curriculum>
<nome>mario</nome>
<cognome>rossi</cognome>
</curriculum>
</radice>
---cv.xsl ---
<?xml version="1.0"?>
<HTML xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<BODY STYLE="font-family:Arial, helvetica, sans-serif; font-size:22pt">
<xsl:for-each select="radice/curriculum">
<DIV STYLE="background-color:teal; color:white; margin-bottom:0.5em">
<xsl:value-of select="nome"/>
<xsl:value-of select="cognome"/>
</DIV>
</xsl:for-each>
</BODY>
</HTML>
--- cv.dtd ---
<!ELEMENT radice (curriculum+)>
<!ELEMENT curriculum (nome+,cognome)>
<!ELEMENT nome (#PCDATA)>
<!ELEMENT cognome (#PCDATA)>