将XML数据插入数据库失败

时间:2016-09-02 18:11:04

标签: php mysql xml

我在尝试将XML数据插入数据库时​​出现问题。过去两天我一直在谷歌上进行研究,但我仍在苦苦挣扎。如果有人能提供帮助,那将非常感激。非常感谢你。

当我尝试插入数据时收到以下错误消息:

  

注意:尝试获取非对象的属性   第13行的insert_into_database.php

     

致命错误:在null中调用成员函数item()   第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>

2 个答案:

答案 0 :(得分:0)

如果您只想获得,请执行以下操作:

$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

    }
}

答案 1 :(得分:0)

考虑使用MySQL的LOAD XML LOCAL INFILE命令批量导入XML文档。这避免了任何循环。但是,结构必须遵循以下类型:

<row column1="value1" column2="value2" .../>

<row>
  <column1>value1</column1>
  <column2>value2</column2>
</row>

<row>
  <field name='column1'>value1</field>
  <field name='column2'>value2</field>
</row>

因此,还要考虑XSLT(将XML文档操作到各种最终用途结构的转换语言)。由于大多数数据都驻留在属性中,因此在XSLT下面(作为字符串嵌入但可以是外部文件)转换为第一个结构:

// LOAD XML AND XSL SOURCES
$doc = new DOMDocument();
$doc->load('data.xml');

$xsl = new DOMDocument;
$xslstr = '<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
            <xsl:output version="1.0" encoding="UTF-8" indent="yes" />
            <xsl:strip-space elements="*"/>

              <xsl:template match="/questions">
                 <xsl:copy>
                    <xsl:apply-templates select="group"/>
                 </xsl:copy>
              </xsl:template>

              <xsl:template match="group">     
                 <xsl:apply-templates select="question"/>     
              </xsl:template>

              <xsl:template match="question"> 
               <xsl:copy>   
                   <xsl:attribute name="id"><xsl:value-of select="ancestor::group/id"/></xsl:attribute>
                   <xsl:attribute name="group"><xsl:value-of select="ancestor::group/@name"/></xsl:attribute>
                   <xsl:attribute name="category"><xsl:value-of select="ancestor::group/category"/></xsl:attribute>
                   <xsl:attribute name="question"><xsl:value-of select="@name"/></xsl:attribute>
                   <xsl:copy-of select="@a|@b|@c|@d"/>
               </xsl:copy>
              </xsl:template>    
            </xsl:transform>';

$xsl->loadXML($xslstr);

// CONFIGURE TRANSFORMER (ENABLE .php_xsl EXTENSION)
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); 

// TRANSFORM SOURCE
$newXml = $proc->transformToXML($doc);

// SAVE TO FILE
$xmlfile = 'output.xml';
file_put_contents($xmlfile, $newXml);

// RUN MYSQL COMMAND (MAY NEED TO ALLOW --local-infile IN SETTINGS)
try {
   $db = new PDO('mysql:host=localhost;dbname=data', 'root', ''); 
   $db->execute("LOAD XML DATA INFILE 'path/to/output.xml'
                 INTO TABLE xml
                 ROWS IDENTIFIED BY '<question>';");
} catch(PDOException $e) {  
    echo $e->getMessage();  
}

已转换的XML (属性名称必须与数据库字段匹配)

<?xml version="1.0" encoding="UTF-8"?>
<questions>
  <question id="1" group="Question Group 1" category="Category A" question="Question 1" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
  <question id="1" group="Question Group 1" category="Category A" question="Question 2" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
  <question id="1" group="Question Group 1" category="Category A" question="Question 3" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
  <question id="1" group="Question Group 1" category="Category A" question="Question 4" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
  <question id="1" group="Question Group 1" category="Category A" question="Question 5" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
  <question id="2" group="Question Group 2" category="Category B" question="Question 1" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
  <question id="2" group="Question Group 2" category="Category B" question="Question 2" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
  <question id="2" group="Question Group 2" category="Category B" question="Question 3" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
  <question id="2" group="Question Group 2" category="Category B" question="Question 4" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
  <question id="2" group="Question Group 2" category="Category B" question="Question 5" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
  <question id="3" group="Question Group 3" category="Category C" question="Question 1" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
  <question id="3" group="Question Group 3" category="Category C" question="Question 2" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
  <question id="3" group="Question Group 3" category="Category C" question="Question 3" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
  <question id="3" group="Question Group 3" category="Category C" question="Question 4" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
  <question id="3" group="Question Group 3" category="Category C" question="Question 5" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
</questions>