最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Editing an XML file with JavaScript? - Stack Overflow

programmeradmin2浏览0评论

Is it possible to get an XML file (not HTML) from a server, add/remove/edit particular parts of it with client-side JavaScript, and then send it back to the server to save it? JSON or any other markup/data interchange format works too.

Is it possible to get an XML file (not HTML) from a server, add/remove/edit particular parts of it with client-side JavaScript, and then send it back to the server to save it? JSON or any other markup/data interchange format works too.

Share Improve this question edited Dec 19, 2020 at 23:50 peterh 1 asked Jun 19, 2009 at 14:31 SkofoSkofo 1432 silver badges7 bronze badges 3
  • Are you going to be saving it back to the server with a server side language (PHP, .NET, etc...)? Or are you also asking if you can save it to the server with javascript? – Nathan Koop Commented Jun 19, 2009 at 14:42
  • Is it even possible to save it to the server with JavaScript? In any case, I plan to use CGI. – Skofo Commented Jun 19, 2009 at 14:57
  • Javascript is a client-side language. But you can use XmlHttpRequest to make a programmatic POST to a CGI script that contains logic to save it locally. – Josh Stodola Commented Jun 19, 2009 at 18:31
Add a ment  | 

5 Answers 5

Reset to default 3

Yes. Using jQuery...

$.get("myGetUrl.php", function(data) {
  var xml = $(data);

  xml.find("myNode").text("newValue");

  $.post("myPostUrl.php", xml, function(resp) {
    alert(resp);
  }, "xml");
});

Yes it's possible. Seach for "XML DOM", and you can edit it on the client quite easily.

Yes. You can read an XML document via AJAX and traverse its DOM like you would with HTML. If you use a framework like jQuery, it's even easier.

Certainly. You can use the XMLHttpRequest object to make the request for the file, do any operations you need to the data, and then post the entire document back using another XMLHttpRequest. You could do this with XML (and that is probably easiest for downloading the original document), but you would probably have the easiest time using JSON for the post back to the server.

You will need a server-side script (i.e. PHP, ASP, Ruby) to receive the posted data, format it however desired (i.e. turn the JSON into an XML document) and save it either as a file or in a database.

This question is far too general to get into specific implementation yet, but if you need additional help with these steps just ask.

Sure. You can use an XMLHttpRequest to fetch an XML document if the server serves it using the text/xml MIME type. The responseText property will give you the XML text, but the browser will also parse the XML for you and provide a DOM tree in responseXML. You can modify that DOM as you please and then serialize it and send it back to the server.

You can also use JSON the same way. You use XMLHttpRequest to get the data from the server, then jsonData = eval(xhr.responseText) to get turn the JSON data into JavaScript objects.

Every major JavaScript library has modules/functions to aid doing either of these methods. XML and JSON are the two most popular data exchange methods in Ajax applications.

发布评论

评论列表(0)

  1. 暂无评论