javascript Xml操作范例

//兼容性 dom转xml字符串
function xmlToString(xmlObj)
{
    if(document.all) //IE浏览器
    {
        return xmlObj.xml;
    }
    else //其他浏览器
    {
        return (new XMLSerializer()).serializeToString(xmlObj);
    }
}

//兼容性创建XmlDom
function createNewXml() 
{ 
var xmlDom = null;   
  if (window.ActiveXObject){   
   xmlDom = new ActiveXObject("Microsoft.XMLDOM");   
   xmlDom.async=false;   
  } 
  else if(document.implementation && document.implementation.createDocument){   
  xmlDom = document.implementation.createDocument("", "", null) 
  }else{   
    xmlDom = null;   
  }   
  return xmlDom;   
} 

//组织Apas_Info dom,并转成xml string(创建办件)
function CJApas_InfoXml(string myserviceid,string myprojectname,string myapplyname,string mymobile,string myphone,string myaddress,string mypostcode,string myreceive_username,string myemail,string mycontactman,string mylegalman,string myidcard,string mycreate_time,string myreceive_time)
{
    var xmlDoc = createNewXml(); 
	//创建两条处理指令     
    var NewPi=xmlDoc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"utf-8\"");     
    xmlDoc.appendChild(NewPi);
	 //创建根元素     
    var apas_info=xmlDoc.createElement("apas_info");     
    xmlDoc.appendChild(apas_info);     	

	//添加子节点
	var serviceid=xmlDoc.createElement("serviceid"); 
	var projectname=xmlDoc.createElement("projectname"); 
	var applyname=xmlDoc.createElement("applyname"); 
	var mobile=xmlDoc.createElement("mobile"); 
	var phone=xmlDoc.createElement("phone"); 
	var address=xmlDoc.createElement("address"); 
	var postcode=xmlDoc.createElement("postcode"); 
	var receive_username=xmlDoc.createElement("receive_username"); 
	var email=xmlDoc.createElement("email"); 
	var contactman=xmlDoc.createElement("contactman"); 
	var legalman=xmlDoc.createElement("legalman"); 
	var idcard=xmlDoc.createElement("idcard"); 
	var create_time=xmlDoc.createElement("create_time"); 
	var receive_time=xmlDoc.createElement("receive_time"); 
	
	apas_info.appendChild(serviceid);    
	apas_info.appendChild(projectname);
	apas_info.appendChild(applyname);
	apas_info.appendChild(mobile);
	apas_info.appendChild(phone);
	apas_info.appendChild(address);
	apas_info.appendChild(postcode);
	apas_info.appendChild(receive_username);
	apas_info.appendChild(email);
	apas_info.appendChild(contactman);
	apas_info.appendChild(legalman);
	apas_info.appendChild(idcard);
	apas_info.appendChild(create_time);
	apas_info.appendChild(receive_time);
	
	var serviceidtext=xmlDoc.createTextNode(myserviceid); 
	var projectnametext=xmlDoc.createTextNode(myprojectname); 
	var applynametext=xmlDoc.createTextNode(myapplyname);
	var mobiletext=xmlDoc.createTextNode(mymobile);
	var phonetext=xmlDoc.createTextNode(myphone);
	var addresstext=xmlDoc.createTextNode(myaddress);
	var postcodetext=xmlDoc.createTextNode(mypostcode);
	var receive_usernametext=xmlDoc.createTextNode(myreceive_username);
	var emailtext=xmlDoc.createTextNode(myemail);
	var contactmantext=xmlDoc.createTextNode(mycontactman);
	var legalmantext=xmlDoc.createTextNode(myserviceid);
	var idcardtext=xmlDoc.createTextNode(mylegalman);
	var create_timetext=xmlDoc.createTextNode(mycreate_time);
	var receive_timetext=xmlDoc.createTextNode(myreceive_time);

	serviceid.appendChild(serviceidtext); 
	projectname.appendChild(projectnametext);
	applyname.appendChild(applynametext);
	mobile.appendChild(mobiletext);
	phone.appendChild(phonetext);
	address.appendChild(addresstext);
	postcode.appendChild(postcodetext);
	receive_username.appendChild(receive_usernametext);
	email.appendChild(emailtext);
	contactman.appendChild(contactmantext);
	legalman.appendChild(legalmantext);
	idcard.appendChild(idcardtext);
	create_time.appendChild(create_timetext);
	receive_time.appendChild(receive_timetext);  

    return xmlToString(xmlDoc);//转成xml string
}
//组织Apas_Info dom,并转成xml string(受理办件)
function SLApas_InfoXml(string myunid,string myaccept_time)
{
    var xmlDoc = createNewXml(); 
	//创建两条处理指令     
    var NewPi=xmlDoc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"utf-8\"");     
    xmlDoc.appendChild(NewPi);
	 //创建根元素     
    var apas_info=xmlDoc.createElement("apas_info");     
    xmlDoc.appendChild(apas_info);     	
	//添加子节点
	var unid=xmlDoc.createElement("unid"); 
	var accept_time=xmlDoc.createElement("accept_time"); 
	
	apas_info.appendChild(unid);    
	apas_info.appendChild(accept_time);
	
	var unidtext=xmlDoc.createTextNode(myunid); 
	var accept_timetext=xmlDoc.createTextNode(myaccept_time); 

	unid.appendChild(unidtext); 
	accept_time.appendChild(accept_timetext);

    return xmlToString(xmlDoc);//转成xml string
}

//组织Apas_Info dom,并转成xml string(办结办件)
function BJApas_InfoXml(string myunid,string mytransact_time,string myhandlestate)
{
    var xmlDoc = createNewXml(); 
	//创建两条处理指令     
    var NewPi=xmlDoc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"utf-8\"");     
    xmlDoc.appendChild(NewPi);
	 //创建根元素     
    var apas_info=xmlDoc.createElement("apas_info");     
    xmlDoc.appendChild(apas_info);     	

	//添加子节点
	var unid=xmlDoc.createElement("unid"); 
	var transact_time=xmlDoc.createElement("transact_time"); 
	var handlestate=xmlDoc.createElement("handlestate"); 
	
	apas_info.appendChild(unid);    
	apas_info.appendChild(transact_time);
	apas_info.appendChild(handlestate);    
	
	var unidtext=xmlDoc.createTextNode(myunid); 
	var transact_timetext=xmlDoc.createTextNode(mytransact_time); 
	var handlestatetext=xmlDoc.createTextNode(myhandlestate);

	unid.appendChild(unidtext); 
	transact_time.appendChild(transact_timetext);
	handlestate.appendChild(handlestatetext);
	
    return xmlToString(xmlDoc);//转成xml string
}

//组织Apas_Log dom,并转成xml string(办结办件)
function BJApas_LogXml(string myWho,string myLog_when)
{
    var xmlDoc = createNewXml(); 
	//创建两条处理指令     
    var NewPi=xmlDoc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"utf-8\"");     
    xmlDoc.appendChild(NewPi);
	 //创建根元素     
    var Apas_Log=xmlDoc.createElement("Apas_Log");     
    xmlDoc.appendChild(Apas_Log);     	

	//添加子节点
	var Who=xmlDoc.createElement("Who"); 
	var Log_when=xmlDoc.createElement("Log_when"); 
	
	Apas_Log.appendChild(Who);    
	Apas_Log.appendChild(Log_when);  
	
	var Whotext=xmlDoc.createTextNode(myWho); 
	var Log_whentext=xmlDoc.createTextNode(myLog_when); 

	Who.appendChild(Whotext); 
	Log_when.appendChild(Log_whentext);
	
    return xmlToString(xmlDoc);//转成xml string
}

//组织Apas_Opinion dom,并转成xml string(办结办件)
function BJApas_OpinionXml(string mybody,string mytype,string myauthor,string mymodified,string mynode_name)
{
    var xmlDoc = createNewXml(); 
	//创建两条处理指令     
    var NewPi=xmlDoc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"utf-8\"");     
    xmlDoc.appendChild(NewPi);
	 //创建根元素     
    var Apas_Opinion=xmlDoc.createElement("Apas_Opinion");     
    xmlDoc.appendChild(Apas_Opinion);     	

	//添加子节点
	var body=xmlDoc.createElement("body"); 
	var type=xmlDoc.createElement("type"); 
	var author=xmlDoc.createElement("author"); 
	var modified=xmlDoc.createElement("modified"); 
	var node_name=xmlDoc.createElement("node_name"); 
	
	Apas_Opinion.appendChild(body);    
	Apas_Opinion.appendChild(type); 
	Apas_Opinion.appendChild(author); 
	Apas_Opinion.appendChild(modified); 
	Apas_Opinion.appendChild(node_name); 	
	
	var bodytext=xmlDoc.createTextNode(mybody); 
	var typetext=xmlDoc.createTextNode(mytype); 
	var authortext=xmlDoc.createTextNode(myauthor); 
	var modifiedtext=xmlDoc.createTextNode(mymodified); 
	var node_nametext=xmlDoc.createTextNode(mynode_name); 

	body.appendChild(bodytext); 
	type.appendChild(typetext);
	author.appendChild(authortext);
	modified.appendChild(modifiedtext);
	node_name.appendChild(node_nametext);
	
    return xmlToString(xmlDoc);//转成xml string
}

以上是javascript对Xml的一些基本操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值