我在尝试将XML数据插入数据库时遇到问题。在过去的两天里,我一直在Google上进行研究,但我仍在努力。如果有人可以提供帮助,将不胜感激。非常感谢你。
尝试插入数据时收到以下错误消息:
注意:尝试获取非对象的属性
第13行的insert_into_database.php
致命错误:在以下位置调用成员函数item()上的null
第13行的insert_into_database.php
insert_into_database.php
<?php
$db = new PDO('mysql:host=localhost;dbname=data', 'root', '');
$xmldoc = new DOMDocument();
$xmldoc->load('data.xml');
$xmldata = $xmldoc->getElementsByTagName('group');
$xmlcount = $xmldata->length;
for ($i=0; $i < $xmlcount; $i++) {
$id = $xmldata->item($i)->getElementsByTagName('id')->item(0)->childNodes->item(0)->nodeValue;
$group = $xmldata->item($i)->getElementsByTagName('group')->item(0)->childNodes->item(0)->nodeValue;
$category = $xmldata->item($i)->getElementsByTagName('category')->item(0)->childNodes->item(0)->nodeValue;
$question = $xmldata->item($i)->getElementsByTagName('question')->item(0)->childNodes->item(0)->nodeValue;
$a = $xmldata->item($i)->getElementsByTagName('a')->item(0)->childNodes->item(0)->nodeValue;
$b = $xmldata->item($i)->getElementsByTagName('b')->item(0)->childNodes->item(0)->nodeValue;
$c = $xmldata->item($i)->getElementsByTagName('c')->item(0)->childNodes->item(0)->nodeValue;
$d = $xmldata->item($i)->getElementsByTagName('d')->item(0)->childNodes->item(0)->nodeValue;
$stmt = $db->prepare("insert into xml values(?,?,?,?,?,?,?,?)");
$stmt->bindParam(1,$group);
$stmt->bindParam(2,$category);
$stmt->bindParam(3,$question);
$stmt->bindParam(4,$a);
$stmt->bindParam(5,$b);
$stmt->bindParam(6,$c);
$stmt->bindParam(7,$d);
$stmt->execute();
printf($name.'<br />');
}
?>
data.xml
<?xml version="1.0" encoding="UTF-8"?>
<questions>
<group name="Question Group 1">
<id>1</id>
<category>Category A</category>
<question name="Question 1" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
<question name="Question 2" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
<question name="Question 3" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
<question name="Question 4" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
<question name="Question 5" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
</group>
<group name="Question Group 2">
<id>2</id>
<category>Category B</category>
<question name="Question 1" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
<question name="Question 2" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
<question name="Question 3" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
<question name="Question 4" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
<question name="Question 5" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
</group>
<group name="Question Group 3">
<id>3</id>
<category>Category C</category>
<question name="Question 1" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
<question name="Question 2" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
<question name="Question 3" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
<question name="Question 4" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
<question name="Question 5" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
</group>
</questions>
最佳答案
如果您只想获取组,请执行以下操作:
$group = $xmldata->item($i)->getAttribute('name');
总体而言,您需要像这样更改代码:
$db = new PDO('mysql:host=localhost;dbname=data', 'root', '');
$xmldoc = new DOMDocument();
$xmldoc->load('data.xml');
$xmldata = $xmldoc->getElementsByTagName('group');
$xmlcount = $xmldata->length;
for($i = 0; $i < $xmlcount; ++$i){
$id = $xmldata->item($i)->getElementsByTagName('id')->item(0)->nodeValue;
$group = $xmldata->item($i)->getAttribute('name');
$category = $xmldata->item($i)->getElementsByTagName('category')->item(0)->nodeValue;
$questioncount = $xmldata->item(0)->getElementsByTagName('question')->length;
for($j = 0; $j < $questioncount; ++$j){
$question = $xmldata->item($i)->getElementsByTagName('question')->item($j)->getAttribute('name');
$a = $xmldata->item($i)->getElementsByTagName('question')->item($j)->getAttribute('a');
$b = $xmldata->item($i)->getElementsByTagName('question')->item($j)->getAttribute('b');
$c = $xmldata->item($i)->getElementsByTagName('question')->item($j)->getAttribute('c');
$d = $xmldata->item($i)->getElementsByTagName('question')->item($j)->getAttribute('d');
// Do your database operations
}
}