繁体   English   中英

如何将XML文件导入mysql db?

[英]how to import XML file to mysql db?

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

/////////////////////////// XML FILE ///////////////////// ///////////////////////////////

<?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 CODE //////////////////// ///////////////////////////////////

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

}

?>

但编号为auto_increment。

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

xml已加载

xml解析

插入mysql

数组([0] =>(“”,“ 2”,“ 1”,“ 1”,“ 2”,“ 2”,“ 2”,“ 2”,“哥斯达黎加布兰卡”,“西班牙”,“ 1 “,” 2d2“,” 2“,” 1“,” 1“,” mmmmm“,” nnnn“,” mmmbbbbbbbbbbbbbbb b bbm“,”“,” dd“,” 222222222.00“,” 1“,” 0“ ,“ 0.00”,“ 2222222222.00”,“ 3”,“ 5”,“ 1”,“”,“”)))

插入mysql

数组([0] =>(“”,“ 2”,“ 1”,“ 1”,“ 2”,“ 2”,“ 2”,“ 2”,“哥斯达黎加布兰卡”,“西班牙”,“ 1 “,” 2d2“,” 2“,” 1“,” 1“,” mmmmm“,” nnnn“,” mmmbbbbbbbbbbbbbbb b bbm“,”“,” dd“,” 222222222.00“,” 1“,” 0“ ,“ 0.00”,“ 2222222222.00”,“ 3”,“ 5”,“ 1”,“”,“”)))

插入mysql

数组([0] =>(“”,“ 2”,“ 1”,“ 1”,“ 2”,“ 2”,“ 2”,“ 2”,“哥斯达黎加布兰卡”,“西班牙”,“ 1 “,” 2d2“,” 2“,” 1“,” 1“,” mmmmm“,” nnnn“,” mmmbbbbbbbbbbbbbbb b bbm“,”“,” dd“,” 222222222.00“,” 1“,” 0“ ,“ 0.00”,“ 2222222222.00”,“ 3”,“ 5”,“ 1”,“”,“”)))

插入mysql

数组([0] =>(“”,“ 2”,“ 1”,“ 1”,“ 2”,“ 2”,“ 2”,“ 2”,“哥斯达黎加布兰卡”,“西班牙”,“ 1 “,” 2d2“,” 2“,” 1“,” 1“,” mmmmm“,” nnnn“,” mmmbbbbbbbbbbbbbbb b bbm“,”“,” dd“,” 222222222.00“,” 1“,” 0“ ,“ 0.00”,“ 2222222222.00”,“ 3”,“ 5”,“ 1”,“”,“”)))

插入的记录:1}

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

采用

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

代替

您可以在链接中找到一个XML2Array类,可以很容易地将xml转换为array,反之亦然。 Xml2Array

用法:

$array = XML2Array::createArray(file_get_contents('path/to/xml-file.xml'));

使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM