73810

Failed to show XML Data with PHP Script

Question:

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>\n"; /*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>\n"; /* the link shown is correct */ $xml = simplexml_load_file($pathtofile); echo gettype($xml) . "<br>\n"; 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>adil.01@wanadoo.fr</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 :)

Answer1:

$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.

Answer2:

I am not exactly sure what you want but I assume you want to insert XML data into your database. If that's the case, try this:

function create_xml() { $xml = simplexml_load_file("file.xml"); $nodes = new SimpleXMLElement('file.xml', null, true) or die("cannot create"); $i = 0; foreach ($nodes->children() as $child) { $var= $child->xml_child; $sql = "INSERT INTO ...)"; mysqli_query($connect, $sql); $i++; echo "xml set"; } }

Recommend

  • Failed to show XML Data with PHP Script
  • Can't ssh to AWS EC2: Identity file not accessible
  • how to get a list with words that are next to a specific word in a string in python
  • Writing a macro that contains a match body
  • How can I update a .yml file, ignoring preexisting Jinja syntax, using Python?
  • Using javascript regex to retrieve year from date string
  • SimpleXML - I/O warning : failed to load external entity
  • Removing back pointers with the use of explicit instantiation declarations causes std::bad_weak_ptr
  • change value of xml node with simpleXML
  • Set runat=“server” from JavaScript
  • How is it possible to modify the whole body part of an xml document loaded in php via simplexml?
  • Given a substitution S and list Xs, how to apply S to Xs
  • Unable to Replicate “R for Beginners” Example
  • How to Eatchar in a multi-line Vim abbreviation
  • Xpath and conditionally selecting descendants based on element value of ancestors
  • Passing command line arguments
  • Use Mockito-core for create mock of final class?
  • F# comparing discriminated unions' by case identifier
  • Why String.ToString()? [duplicate]
  • Task.IsCancelled doesn't work
  • Web.config system.webserver errors
  • Problems installing Yesod for Haskell
  • Loading .coffee files via a view in Rails
  • Unable to install Git-core+svn by MacPorts
  • VSO Build — Response status code does not indicate success: 404 (Not Found)
  • ImageMagick, replace semi-transparent white with opaque white
  • ilmerge with a PFX file
  • Cannot connect to cassandra from Spark
  • Accessing IRQ description array within a module and displaying action names
  • Controls, properties, events and timers running in design time
  • Cross-Platform Protobuf Serialization
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • 0x202A in filename: Why?
  • Getting Messege Twice Using IMvxMessenger
  • File not found error Google Drive API
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs