Jump to content

Count XML child elements and generate HTML table


simzam

Recommended Posts

Hi

I'm trying to count XML elements and generate table row and cell as per result.

XML entries generated by users what I'm trying to do is auto generate table rows and cols and paging them bec if XML file get heavier its hard to get results it slow down processing that's why i appreciate paging  :rtfm::confused::wtf:

 

My XML:

<note>
<a1>Keep holding On - Avril Lavigne </a1>
<a2>Miley Cyrus - Party In The U.S.A </a2>
<a3>Swift Taylor - Breath  </a3>
<a4>Coldplay - The Hardest Part  </a4>
<a5>P!nk - Who Knew  </a5>
<a6>Eminem - Love The Way You Lie ft. Rihanna </a6>
<a7>Nelly Furtado - Say It Right   </a7>
<a8>The Black Eyed Peas - Meet Me Halfway   </a8>
<a9>Justin Bieber - Baby ft. Ludacris  </a9>
<a10>N.E.R.D. - Hot-n-Fun (Official Version) ft. Nelly Furtado  </a10>
</note> 

 

 

 

Link to comment
Share on other sites

Hi

 

I tried my self when i hit http://myurl/test.php?xml=note result not separating all data shows in same cell    :shrug:

 

my PHP:

<?php
error_reporting(-1);


if (!empty($_GET['xml'])) {

$xml = new DOMDocument();
$xml->load("link.xml");
$nodes = $xml->getElementsByTagName($_GET['xml']);

echo <<<EOF
<table border = 1>
        <tr>
                <th>title</th>       
        </tr>

EOF;
foreach($nodes as $node)
{
        echo <<<EOF
        <tr>
                <td>$node->textContent.</td>
        </tr>
EOF;
}
echo '</table>';
}
?>

 

my XML:

<note>
<a1>Keep holding On - Avril Lavigne </a1>
<a2>Miley Cyrus - Party In The U.S.A </a2>
<a3>Swift Taylor - Breath  </a3>
<a4>Coldplay - The Hardest Part  </a4>
<a5>P!nk - Who Knew  </a5>
<a6>Eminem - Love The Way You Lie ft. Rihanna </a6>
<a7>Nelly Furtado - Say It Right   </a7>
<a8>The Black Eyed Peas - Meet Me Halfway   </a8>
<a9>Justin Bieber - Baby ft. Ludacris  </a9>
<a10>N.E.R.D. - Hot-n-Fun (Official Version) ft. Nelly Furtado  </a10>
</note> 

Link to comment
Share on other sites

Close, I adjusted it a bit for you, try this:

 

ini_set ("display_errors", "1"); 
error_reporting(E_ALL);

if(!empty($_GET['xml']) && isset($_GET['xml']))
{
   $xml = $_GET['xml'];
   $dom = new DOMDocument;
   $dom->load('link.xml');
   $xpath = new DOMXPath($dom);
   $xpath->registerNamespace("php", "http://php.net/xpath");
   $nodes = $xpath->query("/{$xml}/*");

   echo "</pre>
<table border="'1'">title";

   foreach ($nodes as $node) 
   {
      echo "";
      echo "" . $node->nodeValue . "";
      echo "";
   }

   echo "</table>";<br>}<br>else<br>{<br>  echo "You did not provide a value";<br>}<br><br>?&g

Link to comment
Share on other sites

will this works like charm but i don't understand  DOMXPath() and registerNamespace kindly help me to understand this explain  :rtfm:

 

XPath is an XML path language that allows you to select nodes and includes some logic and conditions.

registerNamespace is exactly what is sounds like, registering the xpath namespace.

 

 

Link to comment
Share on other sites

thanks !

how to load multi category on different rows like i tried but no luck need your help !

title1 | title2

      |

      |

      |

 

<note>
<source category= "links">
<b1>song 1 </b1>
<b2> song 2 </b2>
<b3>song 3 </b3>
<b4> song 4  </b4>
<b5>  song 5 </b5>
<b6> song 6 </b6>
<b7> song 7   </b7>
<b8>song 8   </b8>
<b9>song 9  </b9>
<b10> song 10  </b10>

</source>

<source category= "title">
<a1>Keep holding On - Avril Lavigne </a1>
<a2>Miley Cyrus - Party In The U.S.A </a2>
<a3>Swift Taylor - Breath  </a3>
<a4>Coldplay - The Hardest Part  </a4>
<a5>P!nk - Who Knew  </a5>
<a6>Eminem - Love The Way You Lie ft. Rihanna </a6>
<a7>Nelly Furtado - Say It Right   </a7>
<a8>The Black Eyed Peas - Meet Me Halfway   </a8>
<a9>Justin Bieber - Baby ft. Ludacris  </a9>
<a10>N.E.R.D. - Hot-n-Fun (Official Version) ft. Nelly Furtado  </a10>
</source>

</note> 

Link to comment
Share on other sites

I put them in 2 separate tables cause I hate dealing with them.

 

   $i = 1;   

   foreach ($nodes as $node) 
   {   
      echo "</pre>
<table border="'1'">title";
      $nodes2 = $xpath->query("/note/source[$i]/*");
      foreach($nodes2 as $node2)
      {   
         echo "";
         echo "" . $node2->nodeValue . "";
         echo "";
      }   
      $i++;
      echo "</table>";<br>   } 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.