如何在HTML中嵌入XML文件? [英] How to embed an XML file in HTML?

查看:749
本文介绍了如何在HTML中嵌入XML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我想过使用JSON并决定采用这种方式,主要是因为使用了逗号(两个项目之间没有逗号,而是列表末尾的逗号) )。

然而,我现在的问题是:我如何嵌入这个XML文件?我是否使用< link rel = /> 标签或其他类似< embed src = /> 标签?我怎么才能从JavaScript中的XML节点读取信息?

解决方案

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

/api.jquery.com/jQuery.parseXML/rel =noreferrer> http://api.jquery.com/jQuery.parseXML/ 展示了一个简单的例子,它在一个xml文档中查找值。

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

  $(函数(){
$ .ajax({
类型:GET,
url:foo.xml,
dataType: xml,
成功:myHandler(xml){
}
});
});


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.

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.).

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?

解决方案

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.

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

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):

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

这篇关于如何在HTML中嵌入XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆