Parsing XML data using php to put into mysql database

前端 未结 5 1473
夕颜
夕颜 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:35

    I personally like the normal XMl formatting so I changed it since its a bit more readable but this is how you can use it:

    $xmlstr = <<
    
    
        
            1
            Cornetto
            1.20
            Traditional Cornetto
        
        
            2
            Smarties
            1.00
            Smarties Icecream
        
    
    
        
            1
            242
            pounds
        
        
            2
            11
            pounds
        
    
    
    XML;
    

    Handling part:

    $xml = new SimpleXMLElement($xmlstr);
    echo 'single value: 
    '; echo $xml->products->product[0]->id; // get single value echo '

    '; //Loop trough multiple products echo 'multiple values:
    '; foreach($xml->products->product as $product) { echo $product->id.' - '; echo $product->name.' - '; echo $product->price.' - '; echo $product->description; echo '
    '; }

提交回复
热议问题