Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

How to show xml data with php

Featured Replies

Hi all

 

This is my first post in this forum and i am new to php with web designing background.

 

The thing is

 

I want to get this type of output using xml and php, please help me

 

<html>

<head></head>

<body>

<table>

<tr>

<td>Heading</td>

<td>Heading</td>

</tr>

<tr>

<td>function name</td>

<td>Shortcut key</td>

</tr>

</table>

 

I can structure in html but have a large content that i want to show in two columns,

so i want to make an xml file and read the data and display in browser.

 

Thanks in advance...

So you have an html 'template' and you want to loop through an XML file and fill in the function name and shortcut key columns?

  • Author

So you have an html 'template' and you want to loop through an XML file and fill in the function name and shortcut key columns?

 

Thanks Jock for your response

 

You got it correct and can you tell me how to do this with php.

 

I hope this is possible with simplexml in php but can't figure out.

It depends really on what your XML schema looks like, here a simple example

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
       <title>Keyboard shortcuts</title>
   </head>
   <body>
<?php

$xmlstring = <<<XML
<?xml version = "1.0" encoding="UTF-8"?>
<document>
   <shortcut>
       <function>Copy</function>
       <keystroke>Ctrl + C</keystroke>
   </shortcut>
   <shortcut>
       <function>Cut</function>
       <keystroke>Ctrl + X</keystroke>    
   </shortcut>
   <shortcut>
       <function>Paste</function>
       <keystroke>Ctrl + V</keystroke>    
   </shortcut>    
</document>
XML;

try {
   $xmli = new SimpleXMLIterator($xmlstring);
   echo "<table>";
   echo "\n\t<caption>List of keyboard shortcuts in Microsoft Excel 97</caption>";    
   echo "\n\t<thead>";
   echo "\n\t\t<tr>";
   echo "\n\t\t\t<th scope=\"col\">Function</th>";
   echo "\n\t\t\t<th scope=\"col\">Keystrokes</th>";
   echo "\n\t\t</tr>";
   echo "\n\t</thead>";
   echo "\n\t<tbody>";
   foreach ( $xmli as $node => $v ) {
       echo "\n\t\t<tr>";
       echo "\n\t\t\t<td>".$v->function."</td>";
       echo "\n\t\t\t<td>".$v->keystroke."</td>";
       echo "\n\t\t</tr>";        
   }
   echo "\n\t</tbody>";    
   echo "\n</table>";
}
catch(Exception $e) {
   echo $e->getMessage();
}
?>
   </body>
</html>

 

In practice you'd probably want to load the xml data from external files, in which case you'd use http://www.php.net/manual/en/function.simplexml-load-file.php

  • Author

It depends really on what your XML schema looks like, here a simple example

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
       <title>Keyboard shortcuts</title>
   </head>
   <body>
<?php

$xmlstring = <<<XML
<?xml version = "1.0" encoding="UTF-8"?>
<document>
   <shortcut>
       <function>Copy</function>
       <keystroke>Ctrl + C</keystroke>
   </shortcut>
   <shortcut>
       <function>Cut</function>
       <keystroke>Ctrl + X</keystroke>    
   </shortcut>
   <shortcut>
       <function>Paste</function>
       <keystroke>Ctrl + V</keystroke>    
   </shortcut>    
</document>
XML;

try {
   $xmli = new SimpleXMLIterator($xmlstring);
   echo "<table>";
   echo "\n\t<caption>List of keyboard shortcuts in Microsoft Excel 97</caption>";    
   echo "\n\t<thead>";
   echo "\n\t\t<tr>";
   echo "\n\t\t\t<th scope=\"col\">Function</th>";
   echo "\n\t\t\t<th scope=\"col\">Keystrokes</th>";
   echo "\n\t\t</tr>";
   echo "\n\t</thead>";
   echo "\n\t<tbody>";
   foreach ( $xmli as $node => $v ) {
       echo "\n\t\t<tr>";
       echo "\n\t\t\t<td>".$v->function."</td>";
       echo "\n\t\t\t<td>".$v->keystroke."</td>";
       echo "\n\t\t</tr>";        
   }
   echo "\n\t</tbody>";    
   echo "\n</table>";
}
catch(Exception $e) {
   echo $e->getMessage();
}
?>
   </body>
</html>

 

In practice you'd probably want to load the xml data from external files, in which case you'd use http://www.php.net/manual/en/function.simplexml-load-file.php

 

Hi Jock good evening,

You provided the xml structure exactly the same way i want.

xml will be external because my brother will add the content to it and he don't know even html.

xml will be easy to him to add content.

 

Your code is working fine.

I made an xml file and load it with simplexml_load_file, working fine in local system, I will upload it and check it online.

 

Thanks again,,,

No problem, if you want to use xml files with that code, just change the $xmli line of code, e.g

 

$xmli = simplexml_load_file('my-file.xml', 'SimpleXMLIterator');

  • 3 weeks later...
  • Author

No problem, if you want to use xml files with that code, just change the $xmli line of code, e.g

 

$xmli = simplexml_load_file('my-file.xml', 'SimpleXMLIterator');

 

Hi Jock, Good afternoon.

 

due to hard disk failure I was unable to access my system for a long time, now every thing is working fine.

I applied your code and everything is going right now and I put it on the server also.

 

I am really amazed for your quick responses thanks again for helping me

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.