Home >Web Front-end >JS Tutorial >How to convert xml to json in javascript
Method to convert xml to json string in javascript: first obtain the XML DOM object through the XML string or request the XML file; then obtain the nodeValue value of the sub-element through traversal and recursion; finally splice out Just a JSON string.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Use JavaScript to convert XML to JSON
First generate the XML DOM object through the XML string:
/** * 通过传入xml的内容字符串来解析xml * @param xmlString xml字符串 * @returns xml的Document对象 */ function getXmlDocumentByXmlString(xmlString) { var xmlDoc = null; if (window.DOMParser) { var parser = new DOMParser(); xmlDoc = parser.parseFromString(xmlString, "text/xml"); } else { //IE xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async = "false"; xmlDoc.loadXML(xmlString); } return xmlDoc; }
Or by requesting the XML file Obtain the DOM object of XML:
/** * 通过传入xml文件路径来解析xml文档 * @param xmlFilePath xml文档路径,如:files/test.xml * @returns xml的Document对象 */ function getXmlDocumentByFilePath(xmlFilePath) { //xmlDocument对象 var xmlDoc = null; //xmlhttp对象 var xmlhttp = null; if (window.XMLHttpRequest) { //IE7+, FireFox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else { //IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.open("GET", xmlFilePath, false); xmlhttp.send(); xmlDoc = xmlhttp.responseXML; return xmlDoc; }
The next step is the key part. By traversing and recursively obtaining the nodeValue of the sub-element, we can splice out the JSON string and convert the XML into a JSON string:
/** * 将XML的Document对象转换为JSON字符串 * @param xmlDoc xml的Document对象 * @return string */ function convertToJSON(xmlDoc) { //准备JSON字符串和缓存(提升性能) var jsonStr = ""; var buffer = new Array(); buffer.push("{"); //获取xml文档的所有子节点 var nodeList = xmlDoc.childNodes; generate(nodeList); /** * 中间函数,用于递归解析xml文档对象,并附加到json字符串中 * @param node_list xml文档的的nodeList */ function generate(node_list) { for (var i = 0; i < node_list.length; i++) { var curr_node = node_list[i]; //忽略子节点中的换行和空格 if (curr_node.nodeType == 3) { continue; } //如果子节点还包括子节点,则继续进行遍历 if (curr_node.childNodes.length > 1) { buffer.push("\"" + curr_node.nodeName + "\": {"); generate(curr_node.childNodes); } else { var firstChild = curr_node.childNodes[0]; if (firstChild != null) { //nodeValue不为null buffer.push("\"" + curr_node.nodeName + "\":\"" + firstChild.nodeValue + "\""); } else { //nodeValue为null buffer.push("\"" + curr_node.nodeName + "\":\"\""); } } if (i < (node_list.length - 2)) { buffer.push(","); } else { break; } } //添加末尾的"}" buffer.push("}"); } jsonStr = buffer.join(""); return jsonStr; }
Usage: Obtain the XML Document object through getXmLDocumentByFilePath(xmlFilePath)
or getXmlDocumentByXmlString(xmlString)
, and then call convertToJSON(xmlDocument )
Pass in the Ducument object of xml to get the converted JSON string.
Applicable scope: Any XML document that does not contain attributes.
For more programming related knowledge, please visit: Programming Video! !
The above is the detailed content of How to convert xml to json in javascript. For more information, please follow other related articles on the PHP Chinese website!