如何在客戶端(JS或JQuery)將JSON轉換為XML [duplicate]

[英]How to convert JSON to XML in Client side(JS or JQuery) [duplicate]


This question already has an answer here:

這個問題已經有了答案:

Is there a possibility to change JSON data to XML in client side(Java sript or in Jquery).

是否有可能在客戶端(Java sript或Jquery)將JSON數據更改為XML。

2 个解决方案

#1


4  

Try using JQuery http://api.jquery.com/jQuery.parseXML/

嘗試使用JQuery http://api.jquery.com/jQuery.parseXML/

You can create an empty xml document like:

您可以創建一個空的xml文檔,比如:

$.parseXML("<xml></xml>") and then set properties on the document got from this.

$. parsexml(“ ”),然后在從中得到的文檔上設置屬性。

An example to demonstrate:

一個例子來說明:

var doc = $.parseXML("<xml/>")
var json = {key1: 1, key2: 2}
var xml = doc.getElementsByTagName("xml")[0]
var key, elem

for (key in json) {
  if (json.hasOwnProperty(key)) {
    elem = doc.createElement(key)
    $(elem).text(json[key])
    xml.appendChild(elem)
  }
}

console.log(xml.outerHTML) // logs <xml><key1>1</key1><key2>2</key2></xml>

#2


0  

Use jQuery.parseJSON to parse JSON and then parseXML() to create an empty XML structure and createElement(),createTextNode() and appendChild() to append nodes to it.

使用jQuery。解析JSON,然后解析parseXML()來創建一個空的XML結構和createElement()、createTextNode()和appendChild()來附加節點。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/02/11/7212d604390d5fee2f21411d5dbaca5b.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com