Parsing XML data using php to put into mysql database

前端 未结 5 1457
夕颜
夕颜 2020-12-05 21:59

I have been asked to parse a simple file which is stored as an XML file, the data is to be then put into a mysql database.

However I have absolutely no clue what to

5条回答
  •  無奈伤痛
    2020-12-05 22:27

    You can use for example SimpleXMLElement and xpath

    
    
     
        
        
     
     
        
        
     
    
    EOF;
    
    $xml=new SimpleXMLElement($xmlStr);
    
    // get product line with xpath for example
    $products=$xml->xpath("/shop/products/product");
    if ($products) {
     // loop over each product node
     foreach ($products as $product) {
       // do whatever you want with the data
       echo("id=>".$product["id"].", name=>".$product["name"]."
    "); } } // same for stock // get product line with xpath for example $stocks=$xml->xpath("/shop/stocks/stock"); if ($stocks) { // loop over each product node foreach ($stocks as $stock) { // do whatever you want with the data echo("id=>".$stock["id"].", amount=>".$stock["amount"]."
    "); } } ?>

提交回复
热议问题