No such namespace prefix: soap12 is in scope on: org.dom4j.tree.DefaultElement

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Jay_1989/article/details/78517991
org.dom4j.IllegalAddException: No such namespace prefix: 

这个异常是说:要添加的这个元素的前缀,没有声明!这主要是在添加元素时直接用上级元素的allElement方法时出现的。例:

Element ns1 = rootTarget.addElement("soap12:Body");      

                     

有两种解决方式:

第一种:新建一个Element,加上前缀声名后再添加到父元素上去。

Element soap12= org.log4j.documentHelper.createElement("soap12:Body");
soap12.add(new Namespace("soap12","http://www.w3.org/2001/XMLSchema"));
rootTarget.add(soap12);   



 第二种:直接在根上声名:然后就可以用addElement方法去添加结点了。
 

Element root = DocumentHelper.createElement("soap12:Envelope").addAttribute("xmlns:soap12","http://www.w3.org/2003/05/soap-envelope").addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance").addAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");  
root.addNamespace("soap12", "http://www.w3.org/2001/XMLSchema");
Document document = DocumentHelper.createDocument(root);  
Element body = root.addElement("soap12:Body");
	     


猜你喜欢

转载自blog.csdn.net/Jay_1989/article/details/78517991
今日推荐