私はこの問題を解決しようとしていますが、私は何か間違っています。誰かがコードを手伝ってくれるかもしれませんXmlからMysqlに致命的なエラー
インターネット上にxmlファイルがあります(例を参照)。そのxmlをPHPの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コードを使用:
Notice: Trying to get property of non-object in C:\xampp\htdocs\xml\xml.php on line 11
Fatal error: Uncaught Error: Call to a member function item() on null in C:\xampp\htdocs\xml\xml.php:11 Stack trace: #0 {main} thrown in C:\xampp\htdocs\xml\xml.php on line 11
私は誰かがこのエラーコードで助けてくれることを願っています マイケル
質問からアクセスキーを削除し、代わりに問題を再現するのに十分なサンプルXMLを追加してください。 – svgrafov
PHPでsimplexml_load_file関数を使用してXMLデータ全体を取得してから、ループ内のすべてのデータをMySqlに追加することができます –