javascript 如何使用jquery创建xml文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7152064/
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-25 23:06:55  来源:igfitidea点击:

How to create xml file with jquery

javascriptjqueryxml

提问by flopifal

Is there a way to create xml file only with jquery/javascript ?

有没有办法只用 jquery/javascript 创建 xml 文件?

回答by Alex Turpin

Not with browser JavaScript, no. You will need some kind of server to write to the file system for you. You could always build the file in JS and then send it through AJAX for the server to write though.

不使用浏览器 JavaScript,不。您将需要某种服务器来为您写入文件系统。您始终可以在 JS 中构建文件,然后通过 AJAX 将其发送给服务器写入。

回答by emboss

Use jQuery.parseXMLto parse a trivial container string:

使用jQuery.parseXML解析一个简单的容器字符串:

var xml = '<?xml version="1.0"?><root/>';
var doc = jQuery.parseXML(xml);

Then you can use normal jQuery DOM manipulation to append nodes to that XML document. Once you need to serialize the final document, you can use the answers to this question.

然后您可以使用普通的 jQuery DOM 操作将节点附加到该 XML 文档。一旦需要对最终文档进行序列化,就可以使用此问题的答案。

回答by Amish Programmer

Your question is more complicated than it would first seem. Let's break it down into two parts:

你的问题比乍看起来更复杂。让我们把它分成两部分:

  1. Can I create a file using javascript?
  2. Can I write XML to that file using jQuery/javascript?
  1. 我可以使用 javascript 创建文件吗?
  2. 我可以使用 jQuery/javascript 将 XML 写入该文件吗?

Unfortunately, the answer to the first question is no. You cannot use javascript running in a browser to access the file system. This has security implications and will not be allowed by major browsers.

不幸的是,第一个问题的答案是否定的。您不能使用在浏览器中运行的 javascript 来访问文件系统。这具有安全隐患,并且不会被主要浏览器允许。

The answer to the first question makes the second one moot. You can create XML in javascript, but you'll have no place to write it.

第一个问题的答案使第二个问题没有实际意义。您可以在 javascript 中创建 XML,但您将无法编写它。

If you provide more information about the reason you want to do this, it may be possible to find alternative solutions to your problem.

如果您提供有关您要这样做的原因的更多信息,则有可能找到解决您问题的替代解决方案。

回答by jiggy

You can create a document like this:

您可以创建这样的文档:

var doc = document.implementation.createDocument(namespace,rootnode,doctype);
doc.documentElement.appendChild(somenode);

And then serialize it to a string:

然后将其序列化为字符串:

new XMLSerializer().serializeToString(doc);

But it will only be in memory. What else do you want to do with it?

但它只会在记忆中。你还想用它做什么?

回答by DaCentaur

Seeing as I'm learning XML myself, this is perhaps an illegal answer as it contains a possible question.

鉴于我自己正在学习 XML,这可能是一个非法答案,因为它包含一个可能的问题。

That said, I'm pretty sure that an XML document can be sent to the browser to display. That can then be explicitly saved by the end-user.

也就是说,我很确定可以将 XML 文档发送到浏览器进行显示。然后可以由最终用户明确保存。

回答by slandau

How about creating it by just using Javascript strings and then using this library?

只使用 Javascript 字符串然后使用这个库来创建它怎么样?

http://plugins.jquery.com/project/createXMLDocument

http://plugins.jquery.com/project/createXMLDocument