无法使用PHP脚本显示XML数据

无法使用PHP脚本显示XML数据,php,mysql,xml,Php,Mysql,Xml,我有大约60000个xml文件需要插入MySQL数据库。因此,我考虑制作一个简单的php脚本,执行一次,从这个xml文件加载数据,并将其插入本地主机上的数据库中 在将其插入我的数据库之前,我试图在页面上的数据上显示它们,但它没有显示任何内容,其类型为NULL 代码如下: <?php $dir = new DirectoryIterator('organismes'); foreach ($dir as $fileinfo) { if (!$fileinfo -> isDot()

我有大约60000个xml文件需要插入MySQL数据库。因此,我考虑制作一个简单的php脚本,执行一次,从这个xml文件加载数据,并将其插入本地主机上的数据库中

在将其插入我的数据库之前,我试图在页面上的数据上显示它们,但它没有显示任何内容,其类型为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文件的一个示例:

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

代理
A2 A3
德莱斯特内尔街34号
01000
布雷斯堡
46.196535
5.2191997
6.
00000000000
00000000000
阿迪尔。01@wanadoo.fr
http://www.adil01.org
所以我试图弄清楚为什么它不能正确显示,为什么它会得到一个空值 所以,兄弟们,如果你能帮上忙,那就太好了:)

紧接着

echo "$A <br />";   
echo“$A
”;
以获得要提取的xml文件的准确表示形式


希望这有帮助。

我不确定您想要什么,但我假设您想要将XML数据插入数据库。如果是这种情况,请尝试以下方法:

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";




    }


}

谢谢marciano,正如你所说的,我想要的是在我的数据库中插入xml数据,但不是只插入空值,为什么在正确加载xml时会这样?我想这是因为xml对象没有任何子对象。您应该添加:$nodes=new simplexmlement('file.xml',null,true)或die(“无法创建”);然后做$nodes->organime->nomi我试过你的方法,但它也不起作用,它什么也没显示,只有空值,我真的不明白:D我想不出来,问题在哪里,谢谢你:)这是我添加到预览代码中的部分:$nodes=new simplexmlement($pathtofile,NULL,true)或die(“无法创建”); $i=0;foreach($nodes->children()as$child){$var=$child->organime->Nom;echo$var.“
”。这应该可以工作了。您在数据库中插入的代码正确吗?您使用了mysql\u查询还是mysqli\u查询?printf($nodes)可以工作吗?请执行
错误报告(~0);ini\u集('display\u errors',1)
在脚本的最开始部分。此外,您还应该检查并遵循错误日志。如果不存在具有此类名称的元素(此处:
Nom
),则会得到NULL。请使用
echo$xml->asXML();
逐字输出xml,并检查您要查找的元素实际放置的位置。
echo "$A <br />";   
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";




    }


}