Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

mysql - Failed to show XML Data with PHP Script

I have about 60000 xml files that i have to insert into a MySQL database. so i thought about making a simple php script that would be executed for once to load data from this xml files and insert it into my db on a localhost.

Before inserting it into my DB i tried to show them on the data on the page but it shows nothing, and its type is NULL.

here is the code :

<?php
$dir = new DirectoryIterator('organismes');
foreach ($dir as $fileinfo) {
   if (!$fileinfo -> isDot()) {
         $XMLFILE = $fileinfo -> getFilename();
         echo $XMLFILE . "<br>
"; /*the filename shows correctly, so the DirectoryIterator is working*/
         $pathtofile = "http://localhost/www/organismes/$XMLFILE"; /*the link to the xml file made with a variable*/
         echo $pathtofile . "<br>
"; /* the link shown is correct */
         $xml = simplexml_load_file($pathtofile);
         echo gettype($xml) . "<br>
";
         if ($xml == FALSE) {
                echo "failed to load xml"; /* this message never shows so the xml file   loads correctly */
         } else {
                $Org = $xml->Organisme->Nom; //this variable $Org gets a NULL Value
                echo $Org . "<br>" ;
                echo gettype($Org);
         }
   }
}
?>

when i used a print_r($xml), it shows some data so the file loads correctly. and here is an example of the xml file that i have :

<Organisme id="adil-01053-01" codeInsee="01053" dateMiseAJour="2013-02-27" pivotLocal="adil">
<Nom>Agence</Nom>    
<EditeurSource>A2 A3</EditeurSource>
<Adresse type="géopostale">
<Ligne>34, rue du Général-Delestraint</Ligne>
<CodePostal>01000</CodePostal>
<NomCommune>Bourg-en-Bresse</NomCommune>
<Localisation>
<Latitude>46.196535</Latitude>
<Longitude>5.2191997</Longitude>
<Précision>6</Précision>
</Localisation>
<Accessibilité type="ACC"/></Adresse>
<CoordonnéesNum>
<Téléphone>00000000000</Téléphone>
<Télécopie>00000000000</Télécopie>
<Email>[email protected]</Email>
<Url>http://www.adil01.org</Url>
</CoordonnéesNum>
 <Ouverture><PlageJ début="vendredi" fin="vendredi"><PlageH début="09:00:00" fin="17:00:00"/></PlageJ><PlageJ début="lundi" fin="jeudi"><PlageH début="09:00:00" fin="18:00:00"/></PlageJ>
</Ouverture>
</Organisme>

so i am trying to figure it out why it doesn't show correctly and why it gets a NULL Value So brothers if you can help that would be wonderful :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
$pathtofile = "http://www.w3schools.com/xml/note.xml";
$xml = simplexml_load_file($pathtofile);
if ($xml == FALSE) {
    echo "failed to load xml";
} 
else
{
    $A = $xml->to;
    echo "$A <br />";                       
}

Focusing on just that part of the code, it seems to work correctly. Either one of two things are happening. The "$xml->Organisme->Nom;" has a spelling error and/or the xml file being pulled does not include those field names.

For testing do

print_r($xml); 

right after

echo "$A <br />";   

to get an accurate representation of the xml file being pulled.

Hope this helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...