【问题标题】:Failed to show XML Data with PHP Script无法使用 PHP 脚本显示 XML 数据
【发布时间】:2013-03-31 15:50:38
【问题描述】:

我有大约 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>

所以我想弄清楚为什么它不能正确显示以及为什么它得到一个 NULL 值 所以兄弟们,如果你能帮忙那就太好了:)

【问题讨论】:

  • 请在脚本的开头添加error_reporting(~0); ini_set('display_errors', 1);。此外,您应该enable error logging 并按照错误日志进行操作。
  • 如果具有这样名称的元素(此处为:Nom)不存在,您将获得 NULL。使用 echo $xml-&gt;asXML(); 逐字输出 XML 并检查您要查找的元素的实际放置位置。

标签: php mysql xml


【解决方案1】:
$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 />";                       
}

只关注代码的那一部分,它似乎可以正常工作。两种情况之一正在发生。 “$xml->Organisme->Nom;”有拼写错误和/或被提取的 xml 文件不包含这些字段名称。

为了测试做

print_r($xml); 

紧接着

echo "$A <br />";   

获取被拉取的 xml 文件的准确表示。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    我不确定您想要什么,但我假设您想将 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 数据,但它只插入 NULL 值,为什么当 xml 正确加载时呢??
    • 我猜这是因为 XML 对象没有任何子对象。您应该添加: $nodes = new SimpleXMLElement('file.xml', null, true) 或 die("cannot create");然后做 $nodes->Organisme->nom
    • 我试过你的方法,但它也不起作用,它什么也没显示,只有 NULL 值,我真的不明白 :D 我想不通,问题出在哪里,谢谢:)
    • 这是我添加到预览代码中的部分:$nodes = new SimpleXMLElement($pathtofile, null, true) or die("cannot create"); $i = 0; foreach ($nodes->children() as $child) { $var= $child->Organisme->Nom;回声 $var。 "
      ";
    • 这应该可以工作。您插入数据库的代码是否正确?你用的是 mysql_query 还是 mysqli_query? printf($nodes) 有效吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-21
    • 2011-08-13
    • 2013-12-01
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多