Hi,
Parsing XML have been a headache for quit a few programmers, including me ;) . So in this post I will give a very simple approach to achieve it. First let us consider an XML file, "simple.xml" with the following content:
<products>
<product>
<name>iPhone</name>
<currency>$</currency>
<price>199.99</price>
</product>
<product>
<name>Galaxy Tab</name>
<currency>$</currency>
<price>299.99</price>
</product>
</products>
Our motive is to navigate through each product and display their details in a listed format such as:
During such parsing all you have to do is to use getElementsByTagName() function smartly. Here there is only 1 "products" tab but multiple "product" tag. So we need to run the loop on the "product" tag.
Here is how I have had done this. Create another file, "example.php" with the following:
<?php
$document = new DOMDocument();
$document->load("simple.xml");
$root = $document->getElementsByTagName("products")->item(0);
$products=$root->getElementsByTagName("product");
echo '<ol>';
foreach ($products as $products_item)
{
$prod_name=$products_item->getElementsByTagName("name")->item(0);
$prod_currency=$products_item->getElementsByTagName("currency")->item(0);
$prod_price=$products_item->getElementsByTagName("price")->item(0);
echo "<li><b>".$prod_name->nodeValue."</b> (".$prod_currency->nodeValue.$prod_price->nodeValue.")</li>";
}
echo '</ol>';
?>
As soon you give the "->item(0)", it referes to the first instance of the tag, hence no looping is required. This is what i have done for "products" tag. As there is only a single instance of it in the entire XML. Similarly under each "product" tag, there is only 1 instance of "name", "currency" and "price" tag hence i have given:
Finally "->nodeValue" returns the value inside the tag. The output generated would be something like:
Best of luck
Parsing XML have been a headache for quit a few programmers, including me ;) . So in this post I will give a very simple approach to achieve it. First let us consider an XML file, "simple.xml" with the following content:
<products>
<product>
<name>iPhone</name>
<currency>$</currency>
<price>199.99</price>
</product>
<product>
<name>Galaxy Tab</name>
<currency>$</currency>
<price>299.99</price>
</product>
</products>
Our motive is to navigate through each product and display their details in a listed format such as:
During such parsing all you have to do is to use getElementsByTagName() function smartly. Here there is only 1 "products" tab but multiple "product" tag. So we need to run the loop on the "product" tag.
Here is how I have had done this. Create another file, "example.php" with the following:
<?php
$document = new DOMDocument();
$document->load("simple.xml");
$root = $document->getElementsByTagName("products")->item(0);
$products=$root->getElementsByTagName("product");
echo '<ol>';
foreach ($products as $products_item)
{
$prod_name=$products_item->getElementsByTagName("name")->item(0);
$prod_currency=$products_item->getElementsByTagName("currency")->item(0);
$prod_price=$products_item->getElementsByTagName("price")->item(0);
echo "<li><b>".$prod_name->nodeValue."</b> (".$prod_currency->nodeValue.$prod_price->nodeValue.")</li>";
}
echo '</ol>';
?>
As soon you give the "->item(0)", it referes to the first instance of the tag, hence no looping is required. This is what i have done for "products" tag. As there is only a single instance of it in the entire XML. Similarly under each "product" tag, there is only 1 instance of "name", "currency" and "price" tag hence i have given:
$prod_name=$products_item->getElementsByTagName("name")->item(0);
$prod_currency=$products_item->getElementsByTagName("currency")->item(0);
$prod_price=$products_item->getElementsByTagName("price")->item(0);
Finally "->nodeValue" returns the value inside the tag. The output generated would be something like:
Best of luck
No comments:
Post a Comment