dongzhu3548 2016-09-02 18:11
浏览 99

将XML数据插入数据库失败

I'm having an issue with trying to insert my XML data into my database. I've spent the past two days researching on google but I'm still struggling. If anyone could help that would be most appreciated. Thank you very much.

I get the following error messages when I try to insert the data:

Notice: Trying to get property of non-object in insert_into_database.php on line 13

Fatal error: Call to a member function item() on null in insert_into_database.php on line 13

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>
  • 写回答

2条回答 默认 最新

  • douyi5822 2016-09-02 18:50
    关注

    If you only want to get the group, then do this:

    $group = $xmldata->item($i)->getAttribute('name');
    

    Overall, you need to change your code like this:

    $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
    
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能