Vi prego aiutamenti perchè sto impazzendo
Io ho questo xml
codice:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">
<item>
<idscheda>1</idscheda>
<titolo>Title of item</titolo>
<author>Author name</author>
<date>Date of article</date>
<article>Content of an article</article>
</item>
<item>
<idscheda>2</idscheda>
<titolo>Title of item 2</titolo>
<author>Author name</author>
<date>Date of article 2</date>
<article>Content of article 2</article>
</item>
</rss>
Vorrei prelevadere i dati idscheda e titolo e inserirli dentro una tabella.
per fare questo utilizzo questo script:
codice:
<?
require "../../_include/mysql_globe4.php";
$cc=999999;
$url = "prova.xml";
//ELEMENTS TO RETRIEVE - YOU MUST KNOW THE TAG NAMES OF EACH ELEMENT - Can be improved using an array (next update):
$idscheda='';
$titolo='';
$author='';
$date='';
$article='';
$link='';
//Counter to start counting the number of items
$cz=0;
$data = file_get_contents($url);
$depth = array();
//The dataset will be stored inside this variable
$txt='';
function startElement($parser, $name, $attrs)
{
//we use global variables to keep track of each element while parsing the whole document
global $cc,$cz,$txt;
global $depth;
global $tagname;
global $idscheda,$titolo,$author,$date,$article,$link;
if($cz < $cc)
{
/* display only if we find all items to display - updated here*/
if(strlen(trim($titolo)) > 0 && strlen(trim($author)) > 0 && strlen(trim($date)) > 0)
{
//at this level we have a NEW ITEM -> we increment to items' counter
if($cz++ >= 1)
{
/*We display the dataset in the order we wish (see below to apply a different design)*/
$txt.=$idscheda.$titolo.$author.$date.$article;
$idscheda='';
$titolo='';
$author='';
$date='';
$article='';
}
}
$tagname=$name;
}
//one level deeper
$depth[$parser]++;
}
function endElement($parser, $name)
{
global $depth;
global $tagname;
$tagname='';
$depth[$parser]--;
}
function characterData($parser, $ddata)
{
global $tagname;
global $cc,$cz,$txt;
global $idscheda,$titolo,$author,$date,$article,$link;
if($cz < $cc)
{
switch($tagname)
{
case 'IDSCHEDA':if(strlen(trim($ddata)) > 0) $idscheda= $ddata;break;
case 'TITOLO':if(strlen(trim($ddata)) > 0) $titolo= $ddata;break;
default: break;
}
require "../../_include/mysql_globe4.php";
/*$db=mysql_connect($dbhost,$dbuser,$dbpw) or die ("Errore durante la connessione al database"); /*
$sql="INSERT INTO xml_esempio (idscheda,titolo) VALUES ('$idscheda','$titolo')";
/*mysql_db_query("creativeglobe4",$sql,$db); */
echo "INSERT INTO xml_esempio (idscheda,titolo) VALUES ('$idscheda','$titolo')";;
echo '
';
}
}
/*everything is triggered here - we parse the xml file and call the startElement & endElement functions, then the characterData function to read all content - the xml_parse line launches the procedure taking care of the arguments*/
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!xml_parse($xml_parser, $data))
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);
//We store the last elements
$txt.=$idscheda.$titolo.$author.$date.$article;
Il problema è che mi inserisce gli idscheda senza problemi ma i titoli no.
Qualcuno mi sa dire dove può essere l'errore.
Non mi stampa alcune errore solo che i titoli proprio non me li inserisce.
Scusate la prolessità.
Grazie.