0

测试.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<propertyList>
  <business  type="food">
  <listingAgent id="1">
    <name>Spiro Abelas</name>
    <email>spiro@abelas.com.au</email>
  </listingAgent>
 </business>
 <business  type="food">
  <listingAgent id="2">
    <name>andy</name>
    <email>andy@abelas.com.au</email>
  </listingAgent>
 </business>
</propertyList>

PHP 代码将 XML 文件解释为对象

<?php
  if (file_exists('test.xml')) {
    $xml = simplexml_load_file('test.xml');
      echo "<pre>";
      print_r($xml);
      echo "</pre>";
  } else {
     exit('Failed to open test.xml.');
     }
 ?>

输出

     SimpleXMLElement Object
(
[business] => Array
(
[0] => SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [type] => food
        )
    [listingAgent] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [id] => 1
                )
            [name] => spiro
            [email] => spiro@abelas.com.au
        )
)
[1] => SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [type] => food
        )
    [listingAgent] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [id] => 2
                )
            [name] => andy
            [email] => andy@abelas.com.au
        )
)))

表结构

在此处输入图像描述

我想将这样的数据插入表格

在此处输入图像描述

但我不知道如何从数组中检索数据并插入多行

所以请指导我如何解决它

4

1 回答 1

1
foreach($xml->business as $row)
{
  $business_type = $row->attributes()->type;
  $id = $row->listingAgent->attributes()->id;
  $name = $row->listingAgent->name;
  $email = $row->listingAgent->email;
  mysql_query('INSERT INTO table…');
}
于 2012-05-29T13:16:33.357 回答