php读取xml文件内容,并循环写入mysql数据库
<?php $dbconn = mysql_connect("localhost","root","root"); $db = mysql_select_db("xbrl",$dbconn); $xml = simplexml_load_file("test.xml"); $name = "" ; $price = 0.00 ; foreach ($xml->children() as $child){ $i = 0 ; foreach ($child->children() as $kid){ if($i==0){ $name = $kid ; $i++ ; }else{ $price = $kid ; $i--; } } $sql = "insert into t_car(name,price) values('$name',$price)" ; mysql_query($sql,$dbconn); } ?>
xml文件内容
<?xml version="1.0" encoding="UTF-8"?> <OBJECT> <CAR> <NAME>BMW</NAME> <PRICE>10000</PRICE> </CAR> <CAR> <NAME>BENZ</NAME> <PRICE>10000</PRICE> </CAR> <CAR> <NAME>TOYOTA</NAME> <PRICE>10000</PRICE> </CAR> <CAR> <NAME>HONDA</NAME> <PRICE>10000</PRICE> </CAR> </OBJECT>