对于clone在结点中的一点见解

来源:互联网 发布:mac版本的office 编辑:程序博客网 时间:2024/05/16 15:20
字体变小 字体变大

这两天都在处理XSD(XML结构定义  XML Schemas Definition ),
在处理中使用了clone的方法,所以在此作一记录

List list = root.getChildren();
for(Iterator iter = list.iterator(); iter.hasNext();)
{
 Element child = (Element)iter.next();
 Element new_child = (Element)child.clone();
 new_root.addContent(new_child);
}
root:根元素
new_root:新的根元素
child:子元素
new_child:新的子元素

Element new_child = (Element)child.clone();
创建了一个新的子元素,这个子元素没有父结点,但存在子结点。
如果:
Element new_child = child;
就会产生错误:
The Content already has an existing parent "root"


 

 
原创粉丝点击