XML解析之javascripe,json,java

function testxml(xml) {
	//"<DATA><PID>03767056</PID><NAME>tudou</NAME></DATA>"
	//string convert xml
	var xmlDoc = null;
	var parser = new DOMParser();
	xmlDoc = parser.parseFromString(xml, "text/xml");
	//query
	var element_pid = xmlDoc.getElementsByTagName("PID")[0];
	var pid_result=element_pid.firstChild.nodeValue;
	//alert(element_pid.childNodes[0].nodeValue);
	//insert
	var root=xmlDoc.getElementsByTagName("DATA")[0];
	var element_age_node=xmlDoc.createElement("AGE");
	var element_age_text=xmlDoc.createTextNode("first");
	root.appendChild(element_age_node);
	element_age_node.appendChild(element_age_text);
	//update
	element_pid.firstChild.nodeValue="123456789";
	//delete
	var element_name = xmlDoc.getElementsByTagName("NAME")[0];
	element_name.parentNode.removeChild(element_name);
	//xml convet string
	var result=(new XMLSerializer()).serializeToString(xmlDoc);
	//alert(result)
	return result;
}

function testjson(json){
	var evalJson=eval('('+json+')');  
	var data=evalJson.DATA;
	var name=data.NAME;
	name='wangwu';
	
	
	var resultRoot={};
	var resultData={};
	resultData.NAME="wangwu";
	resultData.PID="456789";
	resultData.AGE="16";
	resultRoot.DATA=resultData;
	var result=JSON.stringify(resultRoot);
	return result;
}
相关文章
相关标签/搜索