0

任何人都可以帮助将数据从 XML 文档转换为关联数组吗?鉴于 XML 结构,我遇到了问题。代码循环,但重复相同的节点(xml文件中的第一个节点)。此代码插入数据库四行(它们都是同一行)。如何使代码转到 xml 中的另一个节点。

/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////

<?xml version="1.0" encoding="utf-8"?>
<root>
   <property>   
        <id>2</id>
        <type>2</type>
        <rent_type>1</rent_type>
        <cid>1</cid>
        <locid>2</locid>
        <stid>2</stid>
        <cnid>2</cnid>
        <locality>2</locality>
        <state>costa blanca</state>
        <country>spain</country>
        <office_id>1</office_id>
        <mls_id>2d2</mls_id>
        <mls_agent>2</mls_agent>
        <viewad>1</viewad>
        <viewbooking>1</viewbooking>
        <unit_num>mmmmm</unit_num>
        <street_num>nnnn</street_num>
        <address2>mmmbbbbbbbbbbbbbbb b bbm</address2>
        <postcode></postcode>
        <county>dd</county>
        <price>222222222.00</price>
        <showprice>1</showprice>
        <freq>0</freq>
        <bond>0.00</bond>
        <closeprice>2222222222.00</closeprice>
        <priceview>3</priceview>
        <age>5</age>
        <landtype>1</landtype>
        <frontage></frontage>
        <depth></depth>
    </property>
    <property>
        <id>3</id>
        <type>2</type>
        <rent_type>0</rent_type>
        <cid>1</cid>
        <locid>0</locid>
        <stid>0</stid>
        <cnid>0</cnid>
        <locality></locality>
        <state></state>
        <country></country>
        <office_id></office_id>
        <mls_id>3d3</mls_id>
        <mls_agent></mls_agent>
        <viewad>0</viewad>
        <viewbooking>0</viewbooking>
        <unit_num>mmmmm</unit_num>
        <street_num>nnnn</street_num>
        <address2>mmmm</address2>
        <postcode></postcode>
        <county>dd</county>
        <price>3333333333.00</price>
        <showprice>1</showprice>
        <freq>0</freq>
        <bond>0.00</bond>
        <closeprice>333333.00</closeprice>
        <priceview></priceview>
        <age></age>
        <landtype></landtype>
        <frontage></frontage>
        <depth></depth>
     </property>
    <property>
        <id>4</id>
        <type>0</type>
        <rent_type>0</rent_type>
        <cid>1</cid>
        <locid>0</locid>
        <stid>0</stid>
        <cnid>0</cnid>
        <locality></locality>
        <state></state>
        <country></country>
        <office_id></office_id>
        <mls_id>4d4</mls_id>
        <mls_agent></mls_agent>
        <viewad>0</viewad>
        <viewbooking>0</viewbooking>
        <unit_num>mmmmm</unit_num>
        <street_num>nnnn</street_num>
        <address2>mmmm</address2>
        <postcode></postcode>
        <county>dd</county>
        <price>444444444444444.00</price>
        <showprice>1</showprice>
        <freq>0</freq>
        <bond>0.00</bond>
        <closeprice>4444444444444444444.00</closeprice>
        <priceview></priceview>
        <age></age>
        <landtype></landtype>
        <frontage></frontage>
        <depth></depth>
    </property>
   <property>
        <id>5</id>
        <type>0</type>
        <rent_type>0</rent_type>
        <cid>1</cid>
        <locid>0</locid>
        <stid>0</stid>
        <cnid>0</cnid>
        <locality></locality>
        <state></state>
        <country></country>
        <office_id></office_id>
        <mls_id>5d5</mls_id>
        <mls_agent></mls_agent>
        <viewad>0</viewad>
        <viewbooking>0</viewbooking>
        <unit_num>mmmmm</unit_num>
        <street_num>nnnn</street_num>
        <address2>mmmm</address2>
        <postcode></postcode>
        <county>dd</county>
        <price>55555555555555.00</price>
        <showprice>1</showprice>
        <freq>0</freq>
        <bond>0.00</bond>
        <closeprice>55555555555.00</closeprice>
        <priceview></priceview>
        <age></age>
        <landtype></landtype>
        <frontage></frontage>
        <depth></depth>
    </property>
</root> 

/////////////////////////PHP代码//////////////// //////////////////////////////

ini_set('display_errors','On');

echo "starting<br />";

//mysql connection
...
echo "connected to DB<br /><br />";

$url = "s_property1.xml";
    $xmlfgc = file_get_contents($url);
    $xmlitem = new SimpleXMLElement($xmlfgc);
    echo "xml loaded<br /><br />";

foreach ($xmlitem->property as $xml) {
//for ($i=0;$i ;){
    $id          = mysql_real_escape_string($xmlitem->property->id);
    $type        = mysql_real_escape_string($xmlitem->property->type);
    $rent_type   = mysql_real_escape_string($xmlitem->property->rent_type);
    $cid         = mysql_real_escape_string($xmlitem->property->cid);
    $locid       = mysql_real_escape_string($xmlitem->property->locid);
    $stid        = mysql_real_escape_string($xmlitem->property->stid);
    $cnid        = mysql_real_escape_string($xmlitem->property->cnid);
    $locality    = mysql_real_escape_string($xmlitem->property->locality);
    $state       = mysql_real_escape_string($xmlitem->property->state);
    $country     = mysql_real_escape_string($xmlitem->property->country);
    $office_id   = mysql_real_escape_string($xmlitem->property->office_id);
    $mls_id      = mysql_real_escape_string($xmlitem->property->mls_id);
    $mls_agent   = mysql_real_escape_string($xmlitem->property->mls_agent);
    $viewad      = mysql_real_escape_string($xmlitem->property->viewad);
    $viewbooking = mysql_real_escape_string($xmlitem->property->viewbooking);
    $unit_num    = mysql_real_escape_string($xmlitem->property->unit_num);
    $street_num  = mysql_real_escape_string($xmlitem->property->street_num);
    $address2    = mysql_real_escape_string($xmlitem->property->address2);
    $postcode    = mysql_real_escape_string($xmlitem->property->postcode);    
    $county      = mysql_real_escape_string($xmlitem->property->county);
    $price       = mysql_real_escape_string($xmlitem->property->price);
    $showprice   = mysql_real_escape_string($xmlitem->property->showprice);
    $freq        = mysql_real_escape_string($xmlitem->property->freq);
    $bond        = mysql_real_escape_string($xmlitem->property->bond);
    $closeprice  = mysql_real_escape_string($xmlitem->property->closeprice);    
    $priceview   = mysql_real_escape_string($xmlitem->property->priceview);
    $age         = mysql_real_escape_string($xmlitem->property->age);
    $landtype    = mysql_real_escape_string($xmlitem->property->landtype);
    $frontage    = mysql_real_escape_string($xmlitem->property->frontage);
    $depth       = mysql_real_escape_string($xmlitem->property->depth);

echo "xml parsed<br /><br />";

$xmldata = array();

$xmldata[] = '("' . $id. '", "' . $type . '", "' . $rent_type. '", "' . $cid . '", "' . $locid . '", "' . $stid . '", "' . $cnid . '", 
       "' . $locality . '", "' . $state . '", "' . $country . '", "' . $office_id . '", "' . $mls_id . '", "' . $mls_agent . '",
       "' . $viewad . '", "' . $viewbooking . '", "' . $unit_num . '", "' . $street_num . '", "' . $address2 . '", "' . $postcode . '",
       "' . $county . '", "' . $price . '", "' . $showprice . '", "' . $freq . '", "' . $bond . '", "' . $closeprice . '",
       "' . $priceview . '" ,"' . $age . '", "' . $landtype . '", "' . $frontage . '", "' . $depth . '")';

//insert into databse                     
$query = 'INSERT INTO gitfd_ezrealty ( id, type, rent_type, cid, locid, stid, cnid, locality, state, country, office_id, mls_id, mls_agent,
               viewad, viewbooking, unit_num, street_num, address2, postcode, county, price, showprice, freq, bond, closeprice, priceview,
               age, landtype, frontage, depth)
    VALUES' . implode(',', $xmldata);

    mysql_query($query) or die(mysql_error());

    echo "inserted into mysql<br /><br />";

    print_r($xmldata);

}

?>

但 id 是 auto_increment。

此代码的输出 = 4 行进入 db + {开始连接到 DB

已加载 xml

解析的xml

插入mysql

数组([0] =>(“”,“2”,“1”,“1”,“2”,“2”,“2”,“2”,“科斯塔布兰卡”,“西班牙”,“1 "、"2d2"、"2"、"1"、"1"、"mmmmmm"、"nnnn"、"mmmbbbbbbbbbbbbbbbbb b bbm"、""、"dd"、"222222222.00"、"1"、"0" , "0.00", "2222222222.00", "3" ,"5", "1", "", "") ) xml 解析

插入mysql

数组([0] =>(“”,“2”,“1”,“1”,“2”,“2”,“2”,“2”,“科斯塔布兰卡”,“西班牙”,“1 "、"2d2"、"2"、"1"、"1"、"mmmmmm"、"nnnn"、"mmmbbbbbbbbbbbbbbbbb b bbm"、""、"dd"、"222222222.00"、"1"、"0" , "0.00", "2222222222.00", "3" ,"5", "1", "", "") ) xml 解析

插入mysql

数组([0] =>(“”,“2”,“1”,“1”,“2”,“2”,“2”,“2”,“科斯塔布兰卡”,“西班牙”,“1 "、"2d2"、"2"、"1"、"1"、"mmmmmm"、"nnnn"、"mmmbbbbbbbbbbbbbbbbb b bbm"、""、"dd"、"222222222.00"、"1"、"0" , "0.00", "2222222222.00", "3" ,"5", "1", "", "") ) xml 解析

插入mysql

数组([0] =>(“”,“2”,“1”,“1”,“2”,“2”,“2”,“2”,“科斯塔布兰卡”,“西班牙”,“1 "、"2d2"、"2"、"1"、"1"、"mmmmmm"、"nnnn"、"mmmbbbbbbbbbbbbbbbbb b bbm"、""、"dd"、"222222222.00"、"1"、"0" ,“0.00”,“2222222222.00”,“3”,“5”,“1”,“”,“”))

插入的记录:1 }

4

3 回答 3

0
$id          = mysql_real_escape_string($xmlitem->property->id);

利用

$id          = mysql_real_escape_string($xml->id);

反而

于 2013-11-07T15:12:44.373 回答
0

您可以在链接中找到一个类XML2Array,它可以轻松地将 xml 转换为数组,反之亦然。Xml2Array

用法:

$array = XML2Array::createArray(file_get_contents('path/to/xml-file.xml'));
于 2013-11-07T15:16:06.030 回答
0

使用http://php.net/manual/en/function.simplexml-load-file.php

$xml = simplexml_load_file('test.xml');

您应该能够遍历该对象。

或者查看 MySQL 加载 XML 文件的解决方案:http: //dev.mysql.com/doc/refman/5.5/en/load-xml.html

于 2013-11-07T15:16:10.220 回答