0

It`s my code which allows to display xml files in browser after click.I want to display each xml file in table by using php. Can someone tell me how can I do that?

<div id="demo"></div>
<input type="submit" value="pass"  onclick = "loadXMLDoc('a.xml')">
<input type="submit" value="pass"  onclick = "loadXMLDoc('b.xml')">
<script>
function loadXMLDoc(a) {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
           document.getElementById("demo").innerHTML=this.responseText;
        }
    }
    xmlhttp.open("GET", a, true);
    xmlhttp.send();
}
 </script>

xml file

 <?xml version='1.0'?>
    <plan>
        <day>
            <dayname>Monday</dayname>
            <class>
                <startat>8:00</startat>
                <endat>10:00</endat>
                <subject>xxx</subject>
            </class>
            <class>  
                <startat>12:00</startat>
                <endat>13:30</endat>
                <subject>xxx</subject>
            </class>
        </day>
        <day>
            <dayname>Tuesday</dayname>
            <class>
                <startat>10:00</startat>
                <endat>11:30</endat>
                <subject>xxx</subject>
            </class>
            <class>
                <startat>14:00</startat>
                <endat>15:30</endat>
                <subject>xxx</subject>
            </class>
        </day>
      </plan>

It`s timetable. I want to display days next to each other. "dayname" should be in th.

Something like that?

<?php

$xml = simplexml_load_string($xmlstring, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$array = json_decode($json,TRUE);

for ($i=0;$i<$array;$i++) {
    ?>
    <table><tr>
    <?php
    foreach ($xml->day as $day) {
        echo '<th>' . $day->dayname . '</th>';
        foreach ($day->class as $val) {
            echo '<tr><td>'.$val->startat.'</td></tr>';
            echo '<tr><td>'.$val->endtat.'</td></tr>';
            echo '<tr><td>'.$val->subject.'</td></tr>';
        }
       }
      }
    ?>  
   </tr>
   </table>
Kasia1
  • 23
  • 5
  • I want to display xml files in tables in html by using php. Now after click only text is displayed. – Kasia1 Jan 12 '18 at 19:08
  • do you use any loop condition – Suman Jan 12 '18 at 19:09
  • At this site you are expected to try to **write the code yourself**. After **[doing more research](//meta.stackoverflow.com/questions/261592)** if you have a problem you can **post what you've tried** with a **clear explanation of what isn't working** and providing a **[mcve]**. I suggest reading [ask] a good question and [the perfect question](//codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/). Also, be sure to take the [tour] and read **[this](//meta.stackoverflow.com/questions/347937/)**. – Blue Jan 12 '18 at 19:11

1 Answers1

0

Here is the same question from another post. They are using a built-in library and explain how to use it. Link to my reference

$xml = simplexml_load_string($xmlstring, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$array = json_decode($json,TRUE);

Your XML data is now in an array. Then you would display with using just a While loop and build your table that way.

Will
  • 70
  • 9