日期:2014-05-20  浏览次数:20797 次

dom4j中根元素如何写入 冒号
Document doc = DocumentHelper.createDocument(); 
Element document = doc.addElement("document");
Element bbs_lastDate = document.addElement("bbs:lastDate");
bbs_lastDate.setText("aaaa");

我想让它写入一个xml文件里边
<document> 
<bbs:lastDate>aaaa</bbs:lastDate> 
</document>

但是现在犹豫根元素有":"(冒号)所有编译的时候会报错.请问各位大侠这个问题要怎么解决呢?

------解决方案--------------------
把bbs作为xmlns定义,如xmlns:bbs="http://xxx.yyy.com/..."
------解决方案--------------------
帮你顶下,关注中
------解决方案--------------------
抱什么样的错?
你能说的详细些吗?
------解决方案--------------------
org.dom4j.IllegalAddException: No such namespace prefix: ha is in scope on: org.dom4j.tree.DefaultElement@1968e23 [Element: <book attributes: [org.dom4j.tree.DefaultAttribute@14da8f4 [Attribute: name show value "yes"]]/>] so cannot add element: ha:ha
at org.dom4j.tree.AbstractElement.addElement(AbstractElement.java:841)
at com.jtt.tpm.common.ManagerXML_dom4j.addChildNode(ManagerXML_dom4j.java:151)
at com.jtt.tpm.common.ManagerXML_dom4j.main(ManagerXML_dom4j.java:94)


------解决方案--------------------
不懂,up
------解决方案--------------------
up
------解决方案--------------------
ding
------解决方案--------------------
ding
------解决方案--------------------
你不明白我的意思。":"在xml标签中有特殊意义,一般用于指定xmlnamespace,你不能用他来作为标签名的一部份。建议去复习一下xml语法。
------解决方案--------------------
ding
------解决方案--------------------
ding
------解决方案--------------------
你应该在document节点加namespace.
Document doc = DocumentHelper.createDocument(); 
Element document = doc.addElement("document"); 
document.addNamespace("bbs","http://yourdnsname/"); 
Element bbs_lastDate = document.addElement("bbs:lastDate"); 
bbs_lastDate.setText("aaaa"); 

------解决方案--------------------
楼上说的对
------解决方案--------------------
up