Xml到Mysql致命错误

时间:2017-11-27 09:26:38

标签: php mysql xml

我试图让这个工作,但我做错了,我现在尝试2天让它工作,但没有运气。也许有人可以帮我处理代码

我在互联网上有一个xml文件(参见示例),我想用php将xml放在我的mysql中。

这是我的XML文件的示例

<AssignmentItems xmlns="http://server.my.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:schemaLocation="http://server.my.net/static/xsd/datafeed_assignments.xsd" total="29">
<Assignment>
<Id>253049101</Id>
<Status>Enroute</Status>
<Location>EBCI</Location>
<From>EBCI</From>
<Destination>EHGG</Destination>
<Assignment>1 PixCha Passer</Assignment>
<Amount>1</Amount>
<Units>passengers</Units>
<Pay>911.00</Pay>
<PilotFee>0.00</PilotFee>
<Expires>3 days</Expires>
<ExpireDateTime>2017-12-01 06:01:56</ExpireDateTime>
<Type>Trip-Only</Type>
<Express>False</Express>
<Locked>sharee</Locked>
<Comment/>
</Assignment> 
</AssignmentItems>

我使用这个PHP代码将它发送到我的MYSQL

<?php
$db = new PDO('mysql:host=localhost;dbname=test','root','');

$xmldoc = new DOMDocument();
$xmldoc->load('**XML URL**');

$xmldata = $xmldoc->getElementsByTagName('Assignment');
$xmlcount = $xmldata->length;

for  ($i=0; $i < $xmlcount; $i++) {
$Id = $xmldata->item($i)->getElementsByTagName('Id')->item(0)->childNodes->item(0)->nodeValue;
$Status = $xmldata->item($i)->getElementsByTagName('Status')->item(0)->childNodes->item(0)->nodeValue;
$Location = $xmldata->item($i)->getElementsByTagName('Location')->item(0)->childNodes->item(0)->nodeValue;
$Fram = $xmldata->item($i)->getElementsByTagName('From')->item(0)->childNodes->item(0)->nodeValue;
$Destination = $xmldata->item($i)->getElementsByTagName('Destination')->item(0)->childNodes->item(0)->nodeValue;
$Assignment = $xmldata->item($i)->getElementsByTagName('Assignment')->item(0)->childNodes->item(0)->nodeValue;
$Amount = $xmldata->item($i)->getElementsByTagName('Amount')->item(0)->childNodes->item(0)->nodeValue;
$Units = $xmldata->item($i)->getElementsByTagName('Units')->item(0)->childNodes->item(0)->nodeValue;
$Pay = $xmldata->item($i)->getElementsByTagName('Pay')->item(0)->childNodes->item(0)->nodeValue;
$PilotFee = $xmldata->item($i)->getElementsByTagName('PilotFee')->item(0)->childNodes->item(0)->nodeValue;
$Expires = $xmldata->item($i)->getElementsByTagName('Expires')->item(0)->childNodes->item(0)->nodeValue;
$ExpireDateTime = $xmldata->item($i)->getElementsByTagName('ExpireDateTime')->item(0)->childNodes->item(0)->nodeValue;
$Type = $xmldata->item($i)->getElementsByTagName('Type')->item(0)->childNodes->item(0)->nodeValue;
$Express = $xmldata->item($i)->getElementsByTagName('Express')->item(0)->childNodes->item(0)->nodeValue;
$Locked = $xmldata->item($i)->getElementsByTagName('Locked')->item(0)->childNodes->item(0)->nodeValue;
$stmt = $db->prepare("insert into jobs values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
$stmt->bindParam(1, $Id);
$stmt->bindParam(2, $Status);
$stmt->bindParam(3, $Location);
$stmt->bindParam(4, $Fram);
$stmt->bindParam(5, $Destination);
$stmt->bindParam(6, $Assignment);
$stmt->bindParam(7, $Amount);
$stmt->bindParam(8, $Units);
$stmt->bindParam(9, $Pay);
$stmt->bindParam(10, $PilotFee);
$stmt->bindParam(11, $Expires);
$stmt->bindParam(12, $ExpireDateTime);
$stmt->bindParam(13, $Type);
$stmt->bindParam(14, $Express);
$stmt->bindParam(15, $Locked);
$stmt->execute();
printf($Id.'<br/>');
printf($Status.'<br/>');
printf($Location.'<br/>');
printf($Fram.'<br/>');
printf($Destination.'<br/>');
printf($Assignment.'<br/>');
printf($Amount.'<br/>');
printf($Units.'<br/>');
printf($Pay.'<br/>');
printf($PilotFee.'<br/>');
printf($Expires.'<br/>');
printf($ExpireDateTime.'<br/>');
printf($Type.'<br/>');
printf($Express.'<br/>');
printf($Locked.'<br/>');


}

 ?>

我在php页面上遇到此错误:

  

注意:尝试获取非对象的属性   第11行的C:\ xampp \ htdocs \ xml \ xml.php

     

致命错误:未捕获错误:在null上调用成员函数项()   在C:\ xampp \ htdocs \ xml \ xml.php中:11堆栈跟踪:#0 {main}抛出   第11行的C:\ xampp \ htdocs \ xml \ xml.php

我希望有人可以帮助解决此错误代码 迈克尔

2 个答案:

答案 0 :(得分:0)

您只需使用 TextToSpeech t1 =new TextToSpeech(this, new TextToSpeech.OnInitListener() { @Override public void onInit(int status) { } }); public void onPause(){ if(t1 !=null){ t1.stop(); t1.shutdown(); } super.onPause(); } 函数在PHP中获取整个XML数据,然后将simplexml_load_filefor loop添加到数据库中,如下所示:

MySqli Prepared Statements

答案 1 :(得分:0)

我发现了你的错误。当您尝试访问位置1中的项目并且该位置中没有项目时,会注意到该错误: 当您调用此函数时,$xmlcount = $xmldata->length; $xmlcount的值为2,因此您的for循环将使用空数据进行空循环。为了避免这种快速而没有太多控制,请:

$xmlcount = $xmldata->length - 1;

<强> [编辑]

您的XML格式不正确。您错过了<AssignmentItems>

的封闭标记