javascript 在 HTML5 应用程序中使用 XML 文件中的数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4249251/
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
Consume data from XML file in an HTML5 application?
提问by Steve
I have some XML files which are currently used to load data into Adobe Flex applications. I am trying to make an HTML5 version of the application, and I need to load this same data into the html "app" so I can use and manipulate like I would in Flex. I have searched and can't find any information or examples on how to do this, or even if it can be done.
我有一些 XML 文件,目前用于将数据加载到 Adobe Flex 应用程序中。我正在尝试制作应用程序的 HTML5 版本,我需要将这些相同的数据加载到 html“应用程序”中,以便我可以像在 Flex 中一样使用和操作。我已经搜索过,但找不到任何有关如何执行此操作的信息或示例,或者即使可以执行此操作。
For example, if my application is a quiz, I have the questions, choices, and correct answers in an XML file. I would like the HTML file to load this data so it can display my quiz and I can use JS and jQuery to show one question at a time. I just don't know how to "get" the xml file. I suppose I could also convert the xml to json if need be, but it would be easier to use the xml as is.
例如,如果我的应用程序是一个测验,我在 XML 文件中包含问题、选择和正确答案。我希望 HTML 文件加载此数据,以便它可以显示我的测验,并且我可以使用 JS 和 jQuery 一次显示一个问题。我只是不知道如何“获取”xml 文件。我想如果需要,我也可以将 xml 转换为 json,但按原样使用 xml 会更容易。
采纳答案by wajiw
You can do that by using Ajax and a Javascript XML parser. The best way to do that is with a JS library, like jQuery.
您可以通过使用 Ajax 和 Javascript XML 解析器来做到这一点。最好的方法是使用 JS 库,例如 jQuery。
Check out jQuery ajax to grab your XML: http://api.jquery.com/jQuery.ajax/
查看 jQuery ajax 以获取您的 XML:http: //api.jquery.com/jQuery.ajax/
jParse is a pretty decent xml parser: http://jparse.kylerush.net/
jParse 是一个相当不错的 xml 解析器:http://jparse.kylerush.net/
Ajax is for accessing pages in the same domain. If you need to grab a file on a different domain you'll have to find a different solution.
Ajax 用于访问同一域中的页面。如果您需要获取不同域中的文件,则必须找到不同的解决方案。

