Parsing XML data using php to put into mysql database

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

    Assuming the file is called data.xml

    $string = file_get_contents('data.xml') reads the entire file into $string.

    $xml = new SimpleXMLElement($string); parses that string, and converts it into an object tree similar to the actual document. So if that's the document -

    
      
        first
        second
      
    
    

    The SimpleXMLElement object would be used like:

    $xml->b              // gets all children of b (c[0] and c[1])
    print $xml->b->c[0]  // gets the first c, will print "first"
    

提交回复
热议问题