在表中显示xml数据

时间:2018-05-08 11:54:53

标签: javascript jquery html

所以我有一个包含数据的XML,我目前有一个j查询函数,可以获取该数据并将其显示在表中。

movie.xml

<Dataset>
    <Record>
        <Movie_Title>Breath</Movie_Title>
        <Genre>Horror</Genre>
        <Cinema_number>1</Cinema_number>
        <price>8.50</price>
        <Director>Simon Baker</Director>
        <Sessions>
           <Date>
             Monday
             <Time>9:30</Time>
             <Time>11:30</Time>
             <Time>13:30</Time>
             <Time>15:30</Time>
             <Time>17:30</Time>
            </Date>

           <Date>
             Tuesday
             <Time>9:30</Time>
             <Time>11:30</Time>
             <Time>13:30</Time>
             <Time>15:30</Time>
             <Time>17:30</Time>
           </Date>
        </Sessions>
    </Record>
</Dataset>

这就是我目前用于在表格中显示数据的内容

<script language="JavaScript" type="text/javascript">
function loadXMLDoc(dname)
{
    if (window.XMLHttpRequest)
    {
        xhttp=new XMLHttpRequest();
    }
    else
    {
        xhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhttp.open("GET",dname,false);
    xhttp.send();
    return xhttp.responseXML;
} 

  function searchXML()
  {
var xmlDoc = loadXMLDoc("/movie.xml");
var x = xmlDoc.getElementsByTagName("Movie_Title");
var input = document.getElementById("input").value;
var size = input.length;
if (input == "" || input == null)
  {
    document.getElementById("results").innerHTML = "please enter a movie Title!";
    return false;
  } else 
  {

   var divText ="";  
   for (i = 0; i < x.length; i++)
        {
         // Check if movie title is same as value entered in input text
          if (x.length > 0 && xmlDoc.getElementsByTagName("Movie_Title")  [i].childNodes[0].nodeValue == document.getElementById("input").value) 
          {
        var Movie_Title = xmlDoc.getElementsByTagName("Movie_Title") [i].childNodes[0].nodeValue;
        var Genre = xmlDoc.getElementsByTagName("Genre")[i].childNodes[0].nodeValue;
        var date = xmlDoc.getElementsByTagName("Date")[i].childNodes[0].nodeValue;
        var time = xmlDoc.getElementsByTagName("Time")[i].childNodes[0].nodeValue;
        var cinema_number = xmlDoc.getElementsByTagName("Cinema_number")[i].childNodes[0].nodeValue;
        var price = xmlDoc.getElementsByTagName("price")[i].childNodes[0].nodeValue;
        var director = xmlDoc.getElementsByTagName("Director")[i].childNodes[0].nodeValue;

        //appending value of each iterations
        divText += "<h1> The session times are: </h1><br /><table border = 1> <tr><th>Movie Title</th><th>Date</th><th>Time</th><th>Cinema number</th><th>Price</th>"
        + "<tr><td>" + Movie_Title + "</td><td>" + date + "</td><td>" + time + "</td><td>" + cinema_number + "</td><td>" + price + "</td></tr>" + "</table>"; 



      } 


    }

      document.getElementById("results").innerHTML = divText;

    }

  }

这是我的html表

<div class = search_section>
Movie Title <input type="text" name="Movie_Title" id="input">
<br />
<input type="submit" value="Submit" onClick=searchXML()>
<br />
<br />
<div id="results"></div>
</div>

我目前遇到的问题是,当填充表时,它只显示一行而不显示任何其他内容。例如:

电影标题:呼吸 日期:星期一 时间: 9:30 电影院编号: 1 价格: 8.50

我真正想要的是在会话标记中为每个日期填充多行,并且每次都在xml文件中的Date标记内填充。

例如:

电影标题:呼吸 日期:星期一 时间: 11:30 电影院编号: 1 价格: 8.50

电影标题:呼吸 日期:星期一 时间: 13:30 电影院编号: 1 价格: 8.50

......星期一剩下的时间

电影标题:呼吸 日期:周二 时间: 9:30 电影院编号: 1 价格: 8.50

电影标题:呼吸 日期:周二 时间: 11:30 电影院编号: 1 价格: 8.50

......周二剩下的时间

你可以给我任何帮助或想法吗?

2 个答案:

答案 0 :(得分:1)

这是一个有效的解决方案

您需要多个循环。每个电影,每天,每小时都需要循环播放。

function searchXML() {

  var parser, xmlDoc;
  var text = "<Dataset>" + "<Record>" + "<Movie_Title>Breath</Movie_Title>" + "<Genre>Horror</Genre>" + "<Cinema_number>1</Cinema_number>" + "<price>8.50</price>" + "<Director>Simon Baker</Director>" + "<Sessions>" + "<Date>" + "Monday" + "<Time>9:30</Time>" + "<Time>11:30</Time>" + "<Time>13:30</Time>" + "<Time>15:30</Time>" + "<Time>17:30</Time>" + "</Date>" + "<Date>" + "Tuesday" + "<Time>9:30</Time>" + "<Time>11:30</Time>" + "<Time>13:30</Time>" + "<Time>15:30</Time>" + "<Time>17:30</Time>" + "</Date>" + "</Sessions>" + "</Record>" + "<Record>" + "<Movie_Title>Test</Movie_Title>" + "<Genre>Comedy</Genre>" + "<Cinema_number>2</Cinema_number>" + "<price>9.50</price>" + "<Director>Test Baker</Director>" + "<Sessions>" + "<Date>" + "Monday" + "<Time>9:30</Time>" + "<Time>11:30</Time>" + "<Time>15:30</Time>" + "<Time>17:30</Time>" + "</Date>" + "<Date>" + "Wednesday" + "<Time>11:30</Time>" + "<Time>13:30</Time>" + "<Time>15:30</Time>" + "</Date>" + "</Sessions>" + "</Record>" + "</Dataset>";

  parser = new DOMParser();
  xmlDoc = parser.parseFromString(text, "text/xml");
  var x = xmlDoc.getElementsByTagName("Movie_Title");
  var input = document.getElementById("input").value;
  var size = input.length;
  if (input == "" || input == null) {
    document.getElementById("results").innerHTML = "please enter a movie Title!";
    return false;
  } else {
    var divText = "<h1> The session times are: </h1><br /><table border = 1> <tr><th>Movie Title</th><th>Date</th><th>Time</th><th>Cinema number</th><th>Price</th>";
    for (i = 0; i < x.length; i++) {
      var record = xmlDoc.getElementsByTagName("Record")[i];
      var movie_Title = record.getElementsByTagName("Movie_Title")[0].childNodes[0].nodeValue;
      if (x.length > 0 && movie_Title == document.getElementById("input").value) {
        var genre         = record.getElementsByTagName("Genre")[0].childNodes[0].nodeValue;
        var cinema_number = record.getElementsByTagName("Cinema_number")[0].childNodes[0].nodeValue;
        var price         = record.getElementsByTagName("price")[0].childNodes[0].nodeValue;
        var director      = record.getElementsByTagName("Director")[0].childNodes[0].nodeValue;

        for (var dateNum = 0; dateNum < record.getElementsByTagName("Date").length; dateNum++) {
          var date = record.getElementsByTagName("Date")[dateNum].childNodes[0].nodeValue;
          for (var num = 0; num < record.getElementsByTagName("Date")[dateNum].getElementsByTagName("Time").length; num++) {
            var time = record.getElementsByTagName("Time")[num].childNodes[0].nodeValue;

            divText += "<tr><td>" + movie_Title + "</td><td>" + date + "</td><td>" + time + "</td><td>" + cinema_number + "</td><td>" + price + "</td></tr>";
          }
        }
      }
    }
    document.getElementById("results").innerHTML = divText + "</table>";
  }
}
<div class=s earch_section>
  Movie Title
  <br /> (Try: Breath or Test)
  <br />
  <input type="text" name="Movie_Title" id="input" value="Breath">
  <br />
  <input type="submit" value="Submit" onClick="searchXML()">
  <br />
  <br />
  <div id="results"></div>
</div>

答案 1 :(得分:0)

您只是添加符合此条件的元素:

if (x.length > 0 && if (x.length > 0 && xmlDoc.getElementsByTagName("Movie_Title") [i].childNodes[0].nodeValue == document.getElementById("input").value) 

只是您在输入中设置的