javascript 如何在 HTML 中嵌入 XML 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14546698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 21:56:09  来源:igfitidea点击:

How to embed an XML file in HTML?

javascripthtmlxml

提问by Qqwy

I am creating a simple web application for a comic using HTML and JavaScript. Because people that are not very technical should be able to add new comics, I thought about using an XML file as a configuration file.

我正在使用 HTML 和 JavaScript 为漫画创建一个简单的 Web 应用程序。因为技术不是很熟练的人应该可以添加新的漫画,所以就想到了用一个xml文件作为配置文件。

I thought about using JSON and decided against it, mainly because the way comma's are used(no comma between two items breaks it, but a comma at the end of the list also breaks it.).

我考虑过使用 JSON 并决定反对它,主要是因为使用逗号的方式(两个项目之间没有逗号会破坏它,但列表末尾的逗号也会破坏它。)。

However, my question is now: How can I embed this XML file? Do I use the <link rel= />tag, or something else like the <embed src= />tag? And how can I then read information from the XML nodes in JavaScript?

但是,我现在的问题是:如何嵌入这个 XML 文件?我是使用<link rel= />标签还是其他类似<embed src= />标签的东西?以及如何从 JavaScript 中的 XML 节点读取信息?

回答by Mike Sokolov

I would recommend loading a JavaScript library that makes this easy. jQuery is one. If you include jQuery in your page then you use it to load the document and get access to the browser's XML parsing capabilities fairly easily.

我建议加载一个 JavaScript 库,使这变得容易。jQuery 就是其中之一。如果您在页面中包含 jQuery,那么您可以使用它来加载文档并相当容易地访问浏览器的 XML 解析功能。

http://api.jquery.com/jQuery.parseXML/shows a simple example of finding values in an xml document once it's loaded.

http://api.jquery.com/jQuery.parseXML/显示了一个简单的示例,该示例在加载 xml 文档后在其中查找值。

This site http://think2loud.com/224-reading-xml-with-jquery/gives a good example of how to load XML from a remote site. The basic idea is to use AJAX: here's a tiny snippet for posterity that will load foo.xml after the html document has loaded (relies on jQuery):

该站点http://think2loud.com/224-reading-xml-with-jquery/提供了一个很好的示例,说明如何从远程站点加载 XML。基本思想是使用 AJAX:这里有一个小片段供后代使用,它将在 html 文档加载后加载 foo.xml(依赖于 jQuery):

$( function() {
    $.ajax({
        type: "GET",
    url: "foo.xml",
    dataType: "xml",
    success: myHandler(xml) {
    }
    });
});

回答by Iswanto San

Use xmltag. Example :

使用xml标签。例子 :

<html>
  <xml Id = msg>
      <message>
        <to> Visitors </to>
        <from> Author </from>
        <Subject> XML Code Islands </Subject>            
      </message>
  </xml>
</html>