[英]Reading local XML file with javascript
I am new to HTML/Javascript, as well as coding in general so bear with me:).我是 HTML/Javascript 的新手,以及一般的编码,所以请耐心等待:)。 I am trying to create a "Spot the Difference" game in html5 using javascript.
我正在尝试使用 javascript 在 html5 中创建一个“找不同”游戏。 Everything is local (on my machine).
一切都是本地的(在我的机器上)。 I have two pictures, of the same size, one with differences.
我有两张相同大小的图片,一张有差异。 To generate data about the clickable fields, I have a java program that reads both of the images and outputs all of the positions in which pixels are different into a XML file.
为了生成有关可点击字段的数据,我有一个 java 程序,它读取这两个图像并将所有像素不同的位置输出到 XML 文件中。 My plan was to then use this XML file with my javascript to define where the user could click.
然后我的计划是使用这个 XML 文件和我的 javascript 来定义用户可以点击的位置。 However, it appears (correct me if I'm wrong) that javascript cannot read local XML files for security reasons.
但是,出于安全原因,javascript 似乎(如果我错了,请纠正我)无法读取本地 XML 文件。 I do not want to use an ActiveXObject because I plan on putting this onto mobile devices via phone gap or a webkit object.
我不想使用 ActiveXObject,因为我计划通过电话间隙或 webkit object 将它放到移动设备上。 Does anyone have a better approach to this problem, or perhaps a way to read local XML files via javascript?
有没有人有更好的方法来解决这个问题,或者可能是通过 javascript 读取本地 XML 文件的方法? Any help would be greatly appreciated, thanks.
任何帮助将不胜感激,谢谢。
I would suggest modifying your java program to output a JSON formatted file instead of XML.我建议将您的 java 程序修改为 output 一个 JSON 格式的文件,而不是 Z3501BZ7093D39CFEDF8Z 格式的文件JSON is native to JavaScript and will be much simpler for you to load.
JSON 是 JavaScript 原生的,加载起来会更简单。
As for actually loading the data, i'm not sure what the best option is as you say you want to evenutally run this on a mobile device.至于实际加载数据,我不确定最好的选择是什么,因为你说你想最终在移动设备上运行它。 If you were just making a normal website you could setup a web server using either Apache or IIS depending on your OS and put the files in the document root.
如果您只是制作一个普通网站,您可以根据您的操作系统使用 Apache 或 IIS 设置 web 服务器并将文件放在文档根目录中。 Once you've done that you can load the JSON file via Ajax, which can easily be googled for.
完成后,您可以通过 Ajax 加载 JSON 文件,该文件可以很容易地通过 Google 搜索。
Not sure if this helps any.不确定这是否有帮助。
Since this is a local file, you can do this with jQuery由于这是一个本地文件,您可以使用 jQuery 执行此操作
$.ajax({
type: "GET",
url: "your.xml",
dataType: "xml",
success: function(xml){
///do your thing
}
});
http://api.jquery.com/jQuery.ajax/ http://api.jquery.com/jQuery.ajax/
If you are planning to put this into a smart phones (iOS and Android) and read local files, I have done similar things with JSON (yes, please don't use XML).如果您打算将其放入智能手机(iOS 和 Android)并读取本地文件,我已经对 JSON 做过类似的事情(是的,请不要使用 XML)。
Below is the sample to override openFile() in the ContentProvider下面是在 ContentProvider 中覆盖 openFile() 的示例
public ParcelFileDescriptor openFile (Uri uri, String mode) {
try {
Context c = getContext();
File cacheDir = c.getCacheDir();
String uriString = uri.toString();
String htmlFile = uriString.replaceAll(CUSTOM_CONTENT_URI, "");
// Translate the uri into pointer in the cache
File htmlResource = new File(cacheDir.toString() + File.separator + htmlFile);
File parentDir = htmlResource.getParentFile();
if(!parentDir.exists()) {
parentDir.mkdirs();
}
// get the file from one of the resources within the local storage
InputStream in = WebViewContentProvider.class.getResourceAsStream(htmlFile);
// copy the local storage to a cache file
copy(in, new FileOutputStream(htmlResource));
return ParcelFileDescriptor.open(htmlResource, ParcelFileDescriptor.MODE_READ_WRITE);
} catch(Exception e) {
throw new RuntimeException(e);
}
}
I hope it helps我希望它有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.