2 zhangjunbaodbd zhangjunbaodbd 于 2014.08.25 18:24 提问

dom4j节点增加属性问题

如下为在dom4j的节点中增加一个值为null的属性,为什么用asXML()方法显示不出这个属性
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
public static void main(String[] args){
try{
Document document = DocumentHelper.createDocument();
Element ele = document.addElement("abc");
ele.addAttribute("abcName",null);
System.out.println(document.asasXML());
}catch(Exception e){
}
}

1个回答

sutongxuevip
sutongxuevip   2017.11.15 16:46

try{
document = saxReader.read(new File(url));
//得到根节点
Element root = document.getRootElement();
Element hard = root.element(ParentNodes);
List list=hard.elements();
Element ele = DocumentHelper.createElement(node);
ele.addAttribute("id","5");
ele.addAttribute("name",hard_name);
ele.addAttribute("minsize",hard_size);
list.add(0,ele);
XMLWriter writer = new XMLWriter( new FileOutputStream(url), format );
writer.write( document );
writer.close();
}catch (Exception e){
e.printStackTrace();
}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!