Parsing XML data using php to put into mysql database

25,439

Solution 1

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 = <<<XML
<?xml version='1.0' standalone='yes'?>
<shop>
<products>
    <product>
        <id>1</id>
        <name>Cornetto</name>
        <price>1.20</price>
        <description>Traditional Cornetto</description>
    </product>
    <product>
        <id>2</id>
        <name>Smarties</name>
        <price>1.00</price>
        <description>Smarties Icecream</description>
    </product>
</products>
<stocks>
    <stock>
        <id>1</id>
        <amount>242</amount>
        <price>pounds</price>
    </stock>
    <stock>
        <id>2</id>
        <amount>11</amount>
        <price>pounds</price>
    </stock>
</stocks>
</shop>
XML;

Handling part:

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

echo '<br /><br />';

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

Solution 2

You can use for example SimpleXMLElement and xpath

<?php
$xmlStr = <<<EOF
<?xml version="1.0"?>
<shop>
 <products>
    <product id="1" name="Cornetto" price="1.20" description="Traditional Cornetto" />
    <product id="2" name="Smarties" price="1.00" description="Smarties Icecream" />
 </products>
 <stocks>
    <stock id="1" amount="242" price="pounds" />
    <stock id="2" amount="11" price="pounds" />
 </stocks>
</shop>
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"]."<br/>");
 }
}

// 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"]."<br/>");
 }
}

?>

Solution 3

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 -

<root>
  <b>
    <c>first</c>
    <c>second</c>
  </b>
</root>

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"

Solution 4

$xml = simplexml_load_file($filename);
foreach($xml->product as $product) {            

  foreach($product->attributes() as $name => $attribute) {
    echo "$name = $attribute";
  }         
}
Share:
25,439
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    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 do and after looking online all the examples given seem either too complicated for my problem or not the right solution. The XML file looks like this:

    <shop>
    <products>
        <product id="1" name="Cornetto" price="1.20" description="Traditional Cornetto" />
        <product id="2" name="Smarties" price="1.00" description="Smarties Icecream" />
    </products>
    
    <stocks>
        <stock id="1" amount="242" price="pounds" />
        <stock id="2" amount="11" price="pounds" />
    </stocks>
    

    I've tried looking at SimpleXML and I think that's the direction I have to go but I just have no idea.

    Any help or pointers would be great.

  • Matthew
    Matthew over 14 years
    That did nothing for me. I just got an empty page. Maybe I should have mentioned the file had another element it's like this. <shop> <products> </products> <stocks> </stocks> </shop> Would that have made a difference?
  • Sgoettschkes
    Sgoettschkes over 11 years
    You may want to add some more text to describe what you are doing and why.