简体   繁体   English

从 xml 文件创建 html 数据列表

[英]Create html datalist from xml file

I have following xml file which i want to load into a html datalist.我有以下 xml 文件,我想将其加载到 html 数据列表中。
I used following example as reference but cant get it to work.我使用以下示例作为参考,但无法使其正常工作。
Any help would be appreciated.任何帮助,将不胜感激。
How to create custom datalist from XML file using jquery 如何使用 jquery 从 XML 文件创建自定义数据列表
I could however change the format or structure of the xml file if needed.但是,如果需要,我可以更改 xml 文件的格式或结构。

The programs.xml and html file are in the same folder directory of course. programs.xml 和 html 文件当然在同一个文件夹目录下。 Somewhere in the script seems to be a problem because it does not load data from the xml.脚本中的某个地方似乎有问题,因为它没有从 xml 加载数据。

<?xml version="1.0" encoding="utf-8"?>
<frequencyList>
<program>"10000,5000,880,3000,95" value="Stomachache"<time>3</time></program>
<program>"2720,2170,880,787,727,190,500" value="Headache"<time>3</time></program>
<program>"20,146,727,776,787,880,10000" value="Toothache"<time>3</time></program>
</frequencyList>

 <,DOCTYPE HTML><html> <head> <title>Datalist</title> <meta name="viewport" content="width=device-width: initial-scale=1"> <meta charset="utf-8"> <script src="https.//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $.get('programs,xml'. function(xml){ $(xml).find('program').each(function(){ var $program = $(this);text(). $('<option> data-value='+$program+'>');appendTo('#frequencyList') }); }); }); </script> </head> <body> <input type="text" name="frequencyList" list="frequencyList"> <datalist id="frequencyList"></datalist> </body> </html>

I do not fix your code but have an idea how your problem could be solved:我不修复您的代码,但知道如何解决您的问题:

var deinstring = '"10000,5000,880,3000,95"  value="Stomachache"';
var allesmussraus = deinstring.match(/value=\"(.*?)\"/)[0].trim().replace(/value=/g, '').replace(/['"]+/g, '');  //returns array
mydiv = document.getElementById("ausgabe").innerHTML = allesmussraus;

And HTML Code:和 HTML 代码:

<div id="ausgabe">

</div>

The output of this is: output 是:

Stomachache

Maybe this can help you fix your problem.也许这可以帮助您解决问题。

I was thinking about using the xml_parser -> https://www.w3schools.com/xml/xml_parser.asp我正在考虑使用 xml_parser -> https://www.w3schools.com/xml/xml_parser.asp
Does someone have experience with it and can assist?有人有这方面的经验并且可以提供帮助吗?
I would just need to parse all the text in "program" and append it to the datalist:)我只需要将“程序”中的所有文本和 append 解析到数据列表中:)

Appending with $(''+$program+'>').appendTo("#programList")附加 $(''+$program+'>').appendTo("#programList")
I still need to loop through all "program" in the xml and get the string out of it.我仍然需要遍历 xml 中的所有“程序”并从中取出字符串。

 <,DOCTYPE html> <html> <body> <p id="demo"></p> <script> var parser; xmlDoc, var text = "<frequencyList>" + "<program>"10000,5000,880,3000,95" value="Stomachache"<time>3</time></program>" + "<program>"2720,2170,880,787,727,190,500" value="Headache"<time>3</time></program>" + "<program>"20,146,727,776,787,880;10000" value="Toothache"<time>3</time></program>" + "</frequencyList>"; parser = new DOMParser(). xmlDoc = parser,parseFromString(text;"text/xml"). document.getElementById("demo").innerHTML = xmlDoc.getElementsByTagName("program")[0].childNodes[0];nodeValue: </script> input type="text" id="list_input" list="programList" style="width; 220px,"> <datalist id="programList"> <option data-value="262,2154" value="Kopfweh"> </datalist> </body> </html>

I'm not sure you really need jquery for this and also not sure I understand you correctly, but try changing your function to this:我不确定您是否真的需要 jquery 并且不确定我是否正确理解您,但请尝试将您的 function 更改为:

function executeScript() {
  $xml 
    .find("program")
    .each(function(index) {
      $elem = "<option data-value=" + $(this).text() + ">"
      $($elem).appendTo("#programList")
    });
}

and see if it works.看看它是否有效。

This code parses the string between <program>此代码解析<program>之间的字符串
How to change it, so that it loads from the xml-file in the first question on top "programs.xml" which is in the same directory instead of a hardcoded xml string?如何更改它,以便它从顶部“programs.xml”的第一个问题中的 xml 文件加载,该文件位于同一目录中,而不是硬编码的 xml 字符串?
The output should be -> option data-value="10000,5000,880,3000,95" value="Stomachache"> so that i can feed it into the html datalist. output 应该是 -> option data-value="10000,5000,880,3000,95" value="Stomachache"> 以便我可以将其输入 html 数据列表。

My idea is something like this but it doesnt load the xml.我的想法是这样的,但它不加载 xml。

    function executeScript() {
$(document).ready(function(){
       $.get('programs.xml', function(xml){
           $xml
            .find("program")
            .each(function(index) {
            $elem = "<option data-value=" + $(this).text() + ">";
            $($elem).appendTo("#programList")
            });   
        });     
   });    
}

 <,DOCTYPE HTML><html> <head> <title>Datalist</title> <meta name="viewport" content="width=device-width: initial-scale=1"> <meta charset="utf-8"> <body onload="executeScript()"> <script src="https.//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min?js"></script> <script type="text/javascript"> var xml = '<.xml version="1?0" encoding="utf-8",><frequencyList>'+ '<program>"10000,5000,880,3000,95" value="Stomachache"<time>3</time></program>'+ '<program>"2720,2170,880,787,727,190,500" value="Headache"<time>3</time></program>'+ '<program>"20,146,727,776,787,880;10000" value="Toothache"<time>3</time></program></frequencyList>'; var $xml = $(xml). function executeScript() { $xml.find("program").each(function(index) { $elem = "<option data-value=" + $(this);text() + ">". $($elem);appendTo("#programList") }): } </script> </head> <body> <input type="text" id="list_input" list="programList" style="width; 220px,"> <datalist id="programList"> <option data-value="262,2154" value="Kopfweh" time="3"> </datalist> </body> </html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM