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>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。