插入XML数据与PHP的MySQL(insert XML data into mysql with

2019-08-22 09:38发布

xml文件的部分表示问题(xml文件中有数百个客户记录)

    <?xml version="1.0" encoding="utf-8"?>
    <test>
       <customer>   
          <name>customer 1</name>
          <address>address 1</address>
          <city>city 1</city>
          <state>state 1</state>
          <zip>zip 1</zip>
          <phone>phone 1</phone>
          <buyerinfo>
             <shippingaddress>
               <name>ship to</name>
               <address>Ship address1</address>
             </shippingaddress>
           </buyerinfo>
           <shippingDetail>
             <saletax>
               <saletaxamount>2</saletaxamount>
             </saletax>
           </shippingDetail>
       </customer>...

下面是我的代码

 //Xml string is parsed and creates a DOM Document object
    $responseDoc = new DomDocument();        
    $responseDoc->load('test.xml');     
    foreach ($responseDoc->getElementsByTagName('customer') as $customer){

     $sSQL = sprintf(
    "INSERT INTO customer (name, address, city, state, zip, phone, shipto, shipadderss, tax) 
    VALUES ('%s','%s', '%s', '%s','%s','%s', '%s','%s','%s')",
    mysql_real_escape_string($customer->getElementsByTagName('name')->item(0)->nodeValue),
    mysql_real_escape_string($customer->getElementsByTagName('address')->item(0)->nodeValue),
    mysql_real_escape_string($customer->getElementsByTagName('city')->item(0)->nodeValue),
    mysql_real_escape_string($customer->getElementsByTagName('state')->item(0)->nodeValue),
    mysql_real_escape_string($customer->getElementsByTagName('zip')->item(0)->nodeValue),
    mysql_real_escape_string($customer->getElementsByTagName('phone')->item(0)->nodeValue)
    ?
    ?
    ?       
);
$rResult = mysql_query($sSQL);

if(mysql_errno() > 0)
{
    printf(
        '<h4 style="color: red;">Query Error:</h4>
        <p>(%s) - %s</p>
        <p>Query: %s</p>
        <hr />',
        mysql_errno(),
        mysql_error(),
        $sSQL
    );
}

    }

问题:

  1. 我如何获得我的INSERT语句使用mysql_real_escape_string得到customer.buyerinfo.shippingaddress.name节点值? 与表示为“???”

    我有相同的节点名称为“名”两个节点的事实,一个是另一个是命名使它问题使用的getElementsByTagName“名称”标签来获取值。

  2. 同样作为第一个,但我怎么saletaxamount节点数据的价值?

    请好心帮。 谢谢!

Answer 1:

对于XML数据,你有我宁愿SimpleXML扩展,它附带了你需要的一切,这不是那么多的代码来写(这是DOM文档的妹妹)。

因此,在输入数据的每个客户 ,你要取你的9倍左右的值。 你可以制定这些值作为XPath:

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

这种工作原理类似于与数据库查询。 您创建一个包含查询,在XPath语言XML字符串。

我们对SQL做一样都应该去手牵手在一起,所以这里是按照SQL模式:

$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;

这一切都需要现在是打开XML,并指定客户元素上下工夫:

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

然后,你只需要在的foreach每一个客户,获得价值,逃避它们,将它们插入到查询并运行SQL查询(或创建一个包含多个记录一个大的查询):

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
    // ...
}

是的,这是你的代码了。 正如你所看到的,利用阵列,XPath和SQL,您可以简化这个一个伟大的延伸。

对于您的示例XML的第一个客户而言,这则生成以下查询:

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'
  )

整个代码,例如:

$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)
        );
    }
}


Answer 2:

你可以得到子节点的值与内环路shippingaddress节点。

喜欢的东西,之后mysql_real_escape_string($customer->getElementsByTagName('phone')->item(0)->nodeValue)

加:

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)
    }
}

当然,你将不得不使用新值修正查询。

这应该你的工作,对不起,我没有机会测试它



文章来源: insert XML data into mysql with php