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

来源:互联网 发布:网络虚拟礼物 编辑:程序博客网 时间:2024/06/01 09:26
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");     


阅读全文
1 0
原创粉丝点击