insert XML data into mysql with php

拥有回忆 提交于 2019-12-01 14:42:51

For the XML data you have I would prefer the SimpleXML extension, it ships with everything you need and it's not that much code to write (it is DOMDocument's little sister).

So for each customer in the input data, you want to fetch your 9 or so values. You can formulate those values as an xpath:

$values = <<<XPATH
(
    name
    |address
    |city
    |state
    |zip
    |phone
    |buyerinfo/shippingaddress/name
    |buyerinfo/shippingaddress/address
    |shippingDetail/saletax/saletaxamount
)
XPATH;

This works similar to as with a database query. You create a string that contains the query, for XML in the Xpath language.

And we do same for SQL as both should go hand-in-hand together, so here is is the according SQL pattern:

$pattern = <<<SQL
INSERT INTO customer
  (
    name, address, city, state, zip, phone, shipto, shipadderss, tax
  )
  VALUES
  (
    '%s','%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s'
  )
SQL;

All this needs now is to open the XML and specify the customer elements to work on:

$customers = simplexml_load_string($test)->customer; // "test.xml"

Then you only need to foreach over each customer, obtain the values, escape them, insert them into the query and run the SQL query (or create one larger query containing more than one record):

foreach ($customers as $customer) 
{
    $data = $customer->xpath($values);
    $escaped = array_map('mysql_real_escape_string', $data);
    $query = vsprintf($pattern, $escaped);

    // you can now run the query now
    // ...
}

Yes, that is your code already. As you can see, Making use of arrays, xpath and SQL, you can simplify this to a great extend.

For the first customer in your sample-XML this then generates the following query:

INSERT INTO customer
  (
    name, address, city, state, zip, phone, shipto, shipadderss, tax
  )
  VALUES
  (
    'customer 1','address 1', 'city 1', 'state 1', 'zip 1', 'phone 1', 'ship to', 'Ship address1', '2'
  )

The whole code-example:

$values = <<<XPATH
(
    name
    |address
    |city
    |state
    |zip
    |phone
    |buyerinfo/shippingaddress/name
    |buyerinfo/shippingaddress/address
    |shippingDetail/saletax/saletaxamount
)
XPATH;

$pattern = <<<SQL
INSERT INTO customer
  (
    name, address, city, state, zip, phone, shipto, shipadderss, tax
  )
  VALUES
  (
    '%s','%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s'
  )
SQL;

$customers = simplexml_load_string($test)->customer; // "test.xml"

foreach ($customers as $customer)
{
    $data = $customer->xpath($values);
    $escaped = array_map('mysql_real_escape_string', $data);
    $query = vsprintf($pattern, $escaped);

    // you can now run the query now
    $result = mysql_query($query);
    if(mysql_errno())
    {
        printf(
            '<h4 style="color: red;">Query Error:</h4>
            <p>(%s) - %s</p>
            <p>Query:
              <pre>%s</pre>
            </p>
            <hr />',
            mysql_errno(),
            htmlspecialchars(mysql_error()),
            htmlspecialchars($query)
        );
    }
}

you can get child node value with a loop inside shippingaddress node.

Something like, after mysql_real_escape_string($customer->getElementsByTagName('phone')->item(0)->nodeValue)

add:

foreach ($customer->getElementsByTagName('buyerinfo') as $buyerinfo)
{
    foreach ($buyerinfo->getElementsByTagName('shippingaddress') as $shippingaddress)
    {
        mysql_real_escape_string($shippingaddress->getElementsByTagName('name')->item(0)->nodeValue),
        mysql_real_escape_string($shippingaddress->getElementsByTagName('address')->item(0)->nodeValue)
    }
}

of course you will have to fix query with new values.

this should your job, sorry i haven't a chance to test it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!