0

I'm currently working with XMLReader, which I'm using to try and parse certain elements into a MySQL query.

test.xml

<item>
<id>100</id>
<productdata>
  <g:material>paper</g:material>
  <g:size>25cm</g:size>
  <g:price>69</g:price>
</productdata>
<productdata>
  <g:material>paper</g:material>
  <g:size>35cm</g:size>
  <g:price>79</g:price>
</productdata>
</item>
<item>
<id>101</id>
<productdata>
  <g:material>paper</g:material>
  <g:size>25cm</g:size>
  <g:price>69</g:price>
</productdata>
<productdata>
  <g:material>paper</g:material>
  <g:size>35cm</g:size>
  <g:price>79</g:price>
</productdata>
</item>

I want to have the following insert:

INSERT INTO table (id,material,size,price) VALUES (‘100’,’paper’,’25cm’,’69’)
INSERT INTO table (id,material,size,price) VALUES (‘100’,’paper’,’35cm’,’79’)
INSERT INTO table (id,material,size,price) VALUES (‘101’,’paper’,’25cm’,’89’)
INSERT INTO table (id,material,size,price) VALUES (‘101’,’paper’,’35cm’,’99’)

As it turns out now, it’s like this:

INSERT INTO table (id,material,size,price) VALUES (‘100’,’paper’,’25cm’,’69’)
INSERT INTO table (id,material,size,price) VALUES (’69’,’paper’,’35cm’,’79’)
INSERT INTO table (id,material,size,price) VALUES (’79’,’paper’,’25cm’,’89’)
INSERT INTO table (id,material,size,price) VALUES (’89’,’paper’,’35cm’,’99’)

Is it possible to pass the id value to every single insert while looping through the XML?

This is my code:

<?php
$xmlurl = "test.xml";
$xml = new XMLReader;
$xml->open($xmlurl);

$keys = array();
$values = array();
$valuesId = array();

while ($xml->read()) { 
    switch ($xml->nodeType) { 
        case (XMLREADER::ELEMENT):
            if ($xml->localName == "id") {
                $key = trim($xml->localName);
                $xml->read(); 
                $id = trim($xml->value);
                 $value = trim($xml->value); 


                array_push($keys,"`$key`");
                array_push($values,"'$value'");
                break; 
            }
            if ($xml->localName == "material") {
                $key = trim($xml->localName);
                $xml->read(); 
                $value = trim($xml->value); 

                array_push($keys,"`$key`");
                array_push($values,"'$value'");

                break; 
            }
            if ($xml->localName == "size") {
                $key = trim($xml->localName);
                $xml->read(); 
                $value = trim($xml->value); 

                array_push($keys,"`$key`");
                array_push($values,"'$value'");

                break; 
            }
            if ($xml->localName == "price") {
                $key = trim($xml->localName);
                $xml->read(); 
                if($xml->value != 0) $value = trim($xml->value); 

                array_push($keys,"`$key`");
                array_push($values,"'$value'");

                break; 
            }
    }
    $valuepack = array_chunk($values, 4);   

}
$keys = array_unique($keys); 
$table = "tablename";

foreach($valuepack as $ValAllId => $tablevalueall) {            
    $SQL = "";
    $SQL .= "INSERT INTO $table (";
    $SQL .= implode(", ",$keys);
    $SQL .= ") VALUES (";
    $SQL .= implode(", ",$tablevalueall);
    $SQL .= ");<br />";
    echo $SQL;
}
?>

Thanks!

J

4

0 回答 0