Hi,

Can any one provide me small code or way how can I read the below XML file in PHP.

<Countries>
<Country>
    <Page1_col1>Country1</Page1_col1>
    <Page1_col2>89,921</Page1_col2>
    <Page1_col2>61</Page1_col2>

    <Country_sub_details>
        <Page2_col1>A1</Page2_col1>
        <Page2_col2>1,187</Page2_col2>
        <Page2_col3>45</Page2_col3>

        <Country_sub_sub_details>
                <Page3_col1>AA1</Page3_col1>
                <Page3_col2>375</Page3_col2>
                <Page3_col3>45</Page3_col3>
        </Country_sub_sub_details>
    </Country_sub_details>
</Country>
</Countries>

I am not aware about the XML libraries in PHP.

Hi
You can read easily an XML document in PHP with simpleXML, it takes all the XML elements as a hierarchical tree.
Here is a code for your XML (For more details and examples with simpleXML, see this tutorial: http://www.coursesweb.net/php-mysql/php-simplexml ):

<?php
$xml = '<Countries>
  <Country>
    <Page1_col1>Country1</Page1_col1>
    <Page1_col2>89,921</Page1_col2>
    <Page1_col2>61</Page1_col2>
    <Country_sub_details>
    <Page2_col1>A1</Page2_col1>
    <Page2_col2>1,187</Page2_col2>
    <Page2_col3>45</Page2_col3>
    <Country_sub_sub_details>
    <Page3_col1>AA1</Page3_col1>
    <Page3_col2>375</Page3_col2>
    <Page3_col3>45</Page3_col3>
    </Country_sub_sub_details>
    </Country_sub_details>
  </Country>
</Countries>';
//  load the content of the XML file and create a new XML object
$xmlobj = simplexml_load_string($xml);

// get and prints the name of the first element (root)
echo 'root: '. $xmlobj->getName() . '<br />';

// loops through the children of root
foreach($xmlobj->children() as $child) {
  echo $child->getName(). ': '. '<br />';          // print the name of the current child

  // if the child node has other children
  if($child->count()>1) {
    // loops through the child node, prints the name and the content of each $child2
    foreach($child as $child2) {
     echo $child2->getName(). ': '. $child2. '<br />';

      // if the child2 node has other children
      if($child2->count()>1) {
        // loops through the child2 node, prints the name and the content of each $child3
        foreach($child2 as $child3) {
         echo $child3->getName(). ': '. $child3. '<br />';

          // if the child3 node has other children
          if($child3->count()>1) {
            // loops through the child3 node, prints the name and the content of each $child4
            foreach($child3 as $child4) {
             echo $child4->getName(). ': '. $child4. '<br />';
            }
          }
        }
      }
    }
  }
}
?>

Thanks MarPlo I have done with your help but now I need the result in the following way.

Country1  89,921  61
A1        1187  45
AA1       375  45

How can I read that in this format.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.