PHP解析XML文件 并存入数据库

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

你可能感兴趣的:(sql,PHP,mysql,xml)