dom创建xml文件报“java.lang.IllegalStateException: Root element not set”

错误如下:

  

dom创建xml文件报“java.lang.IllegalStateException: Root element not set”_第1张图片

 

 

错误代码如下:

 

doc = XmlUtil.CreateDocument();
		if(doc == null) return "";
	    root = doc.createElement("transaction");  //创建根元素
	    doc.setXmlVersion("1.0");	        
	    root.setAttribute("xmlns", "http://www.cebbank.com/wangshangyh/ebankToBiz");
	    root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
	    root.setAttribute("xsi:schemaLocation", "http://www.cebbank.com/wangshangyh/ebankToBiz schema_v0.5.5.xsd");
	    Element transfer = XmlUtil.appendChild("balance", root, doc); 
	    Element transferResponse = XmlUtil.appendChild("balanceRespond", transfer, doc);
	    Element transferResponseHeader = XmlUtil.appendChild("balanceRespondHeader", transferResponse, doc);
	    XmlUtil.appendChild("language", getLanguage(), transferResponseHeader, doc);
	    XmlUtil.appendChild("respondTime", getResponseTime(), transferResponseHeader, doc);
		Element responseStatus = XmlUtil.appendChild("respondStatus", transferResponseHeader, doc);
		XmlUtil.appendChild("respondCode", getResponseStatus().getResponseCode(), responseStatus, doc);
		XmlUtil.appendChild("respondInfo", getResponseStatus().getResponseInfo(), responseStatus, doc);
		XmlUtil.appendChild("userID", getUserID(), transferResponseHeader, doc);
		XmlUtil.appendChild("batchID", getBatchID(), transferResponseHeader, doc);
		XmlUtil.appendChild("transPatches", getTransPatches(), transferResponseHeader, doc);

 

原因:创建的根元素没有指定到doc文件里,所以程序没有把元素和doc联系在一起。

 

改正代码如下:

   

doc = XmlUtil.CreateDocument();
		if(doc == null) return "";
	    root = doc.createElement("transaction");  //创建根元素
	    doc.appendChild(root);    //把根元素与doc联系起来
	    doc.setXmlVersion("1.0");	        
	    root.setAttribute("xmlns", "http://www.cebbank.com/wangshangyh/ebankToBiz");
	    root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
	    root.setAttribute("xsi:schemaLocation", "http://www.cebbank.com/wangshangyh/ebankToBiz schema_v0.5.5.xsd");
	    Element transfer = XmlUtil.appendChild("balance", root, doc); 
	    Element transferResponse = XmlUtil.appendChild("balanceRespond", transfer, doc);
	    Element transferResponseHeader = XmlUtil.appendChild("balanceRespondHeader", transferResponse, doc);
	    XmlUtil.appendChild("language", getLanguage(), transferResponseHeader, doc);
	    XmlUtil.appendChild("respondTime", getResponseTime(), transferResponseHeader, doc);
		Element responseStatus = XmlUtil.appendChild("respondStatus", transferResponseHeader, doc);
		XmlUtil.appendChild("respondCode", getResponseStatus().getResponseCode(), responseStatus, doc);
		XmlUtil.appendChild("respondInfo", getResponseStatus().getResponseInfo(), responseStatus, doc);
		XmlUtil.appendChild("userID", getUserID(), transferResponseHeader, doc);
		XmlUtil.appendChild("batchID", getBatchID(), transferResponseHeader, doc);
		XmlUtil.appendChild("transPatches", getTransPatches(), transferResponseHeader, doc);


 

 


 

你可能感兴趣的:(dom创建xml文件报“java.lang.IllegalStateException: Root element not set”)