无法使用 PHP 脚本显示 XML 数据 [英] Failed to show XML Data with PHP Script
问题描述
我有大约 60000 个 xml 文件必须插入到 MySQL 数据库中.所以我想制作一个简单的 php 脚本,该脚本将被执行一次以从这个 xml 文件加载数据并将其插入到本地主机上的我的数据库中.
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.
在将它插入我的数据库之前,我试图在页面上的数据上显示它们,但它什么也没显示,它的类型是 NULL.
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.
代码如下:
<?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);
}
}
}
?>
当我使用 print_r($xml) 时,它会显示一些数据,以便文件正确加载.这是我拥有的 xml 文件的示例:
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>
所以我想弄清楚为什么它不能正确显示以及为什么它会得到一个 NULL 值所以兄弟们,如果你能帮忙那就太好了:)
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 :)
推荐答案
我不确定您想要什么,但我假设您想将 XML 数据插入到您的数据库中.如果是这样的话,试试这个:
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";
}
}
这篇关于无法使用 PHP 脚本显示 XML 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!