DOM 解析 xml 文件无缘无故变大,如何解决?? 输出时不时可以用 Format 的么? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /* * Created on 2005-7-2 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */package cn.ybbkd2;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.text.DateFormat;import java.util.Calendar;import java.util.Date;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.apache.crimson.tree.XmlDocument;import org.w3c.dom.Document;import org.w3c.dom.Element;/** * @author Administrator * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */public class SiteUserConfig { private File cfg; private DocumentBuilderFactory dbf; private DocumentBuilder db; private Document doc; private Element configroot; public SiteUserConfig() throws Exception { this.dbf = DocumentBuilderFactory.newInstance(); this.db = dbf.newDocumentBuilder(); this.cfg = new File ("ybbkd2docs/config.xml"); if(cfg.exists()) { this.doc = db.parse(cfg); //this.doc.normalize(); this.configroot = doc.getDocumentElement(); } else { this.doc = db.newDocument(); this.doc.appendChild(doc.createComment("站点设置")); this.configroot = doc.createElement("config"); this.doc.appendChild(this.configroot); BaseUser admin = new BaseUser(); admin.setUserName("ybbkd2"); admin.setPassword("721521"); admin.setEmail("[email protected]"); if (!this.CreateUser(admin)) { throw new Exception("001 配置文件不存在,重新建立时出现错误 ,管理员帐号建立错误."); } } } public boolean CreateUser (BaseUser user) { Element newuser = doc.createElement("user"); newuser.setAttribute("username",user.getUserName()); newuser.setAttribute("password",user.getPassword()); newuser.setAttribute("email",user.getEmail()); //保存建立时间 Calendar now = Calendar.getInstance(); Date date = now.getTime(); DateFormat df = DateFormat.getDateInstance(); String sdate = df.format(date); newuser.setAttribute("createdate",sdate); Element newgb = doc.createElement("guestbook"); newgb.setAttribute("current-id","1"); Element newdoc = doc.createElement("document"); newdoc.setAttribute("current-id","1"); //构建树 this.configroot.appendChild(newuser); newuser.appendChild(newgb); newuser.appendChild(newdoc); //保存到磁盘,失败 return false try { ((XmlDocument)doc).write(new FileOutputStream(cfg)); } catch (IOException e) { return false; } return true; } }运行几次之后XML为:<?xml version="1.0" encoding="UTF-8"?><!--站点设置--><config> <user username="ybbkd2" password="721521" email="[email protected]" createdate="2005-7-2"> <guestbook current-id="1" /> <document current-id="1" /> </user> <user username="hotmail" password="123" email="[email protected]" createdate="2005-7-2"> <guestbook current-id="1" /> <document current-id="1" /> </user> <user username="hotmail" password="123" email="[email protected]" createdate="2005-7-2"> <guestbook current-id="1" /> <document current-id="1" /> </user> <user username="hotmail" password="123" email="[email protected]" createdate="2005-7-2"> <guestbook current-id="1" /> <document current-id="1" /> </user> <user username="hotmail" password="123" email="[email protected]" createdate="2005-7-2"> <guestbook current-id="1" /> <document current-id="1" /> </user> <user username="hotmail" password="123" email="[email protected]" createdate="2005-7-2"> <guestbook current-id="1" /> <document current-id="1" /> </user> <user username="hotmail" password="123" email="[email protected]" createdate="2005-7-7"> <guestbook current-id="1" /> <document current-id="1" /> </user></config>只要添加一条记录空行就会增大一行,请问大家有什么好方法么? // ---------- 调用类 SiteUserConfig --------------public class Run { public static void main(String[] args) { SiteUserConfig cfg=null; try { cfg = new SiteUserConfig(); } catch (Exception e) { System.out.println(e.getMessage()); return; } BaseUser user = new BaseUser(); user.setEmail("[email protected]"); user.setPassword("123"); user.setUserName("hotmail"); cfg.CreateUser(user); }} this.configroot.appendChild(newuser); newuser.appendChild(newgb); newuser.appendChild(newdoc);这里我觉得有问题,按下面改改试试: newuser.appendChild(newgb); newuser.appendChild(newdoc); this.configroot.appendChild(newuser); 我也遇到了同样的问题。我是做xml文件的查询。然后如果文件中没有查询的数据,则添加一行新的数据。也出现了和个问题。我只好使用javabean的方法解决了。 to rower203(华仔) : 你说的我试了,不行啊,还是老样子to victorppy(键盘上的鱼) 能不能说的详细点阿? 修剪一下,让它对齐,write 方法没有 类似 Format.PRETTY 之类的boolean型或枚举型参数?我记得 jdom 写出的时候有个参数 表示 要 自动缩进 格式 。不过不知道会不会多出空行来。 我说:this.configroot.appendChild(newuser);newuser.appendChild(newgb);newuser.appendChild(newdoc);这里我觉得有问题,按下面改改试试:newuser.appendChild(newgb);newuser.appendChild(newdoc);this.configroot.appendChild(newuser);此处逻辑上有问题,你又得到了结果说明其他地方逻辑上也会有问题。你要自己查查程序了,这里只能给个提示,问题还要自己去解决的。 今天没买找回家的票 初看struts2.0例子问题, Hibernate问题:QuerySyntaxError: User is not mapped,帮帮忙吧 新手问题 急急急!!!!!在线等 在ejb中调用其它类,说找不到路径,在线等待,解决即给分 怎样通过实体bean修改表中的数据? 请问那里有comm.jar下载 初学xml疑问 关于线程和定时器timer的问题 Weblogic服务器,在EJB中用JNDI查找连接池的问题 用dom4j如何在XML文件中插入类似"<?xml-stylesheet type="text/xsl" href="demo.xsl"?>"这一行?
* Created on 2005-7-2
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package cn.ybbkd2;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;import org.apache.crimson.tree.XmlDocument;
import org.w3c.dom.Document;
import org.w3c.dom.Element;/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class SiteUserConfig {
private File cfg;
private DocumentBuilderFactory dbf;
private DocumentBuilder db;
private Document doc;
private Element configroot;
public SiteUserConfig() throws Exception {
this.dbf = DocumentBuilderFactory.newInstance();
this.db = dbf.newDocumentBuilder();
this.cfg = new File ("ybbkd2docs/config.xml"); if(cfg.exists()) {
this.doc = db.parse(cfg);
//this.doc.normalize();
this.configroot = doc.getDocumentElement();
}
else {
this.doc = db.newDocument();
this.doc.appendChild(doc.createComment("站点设置"));
this.configroot = doc.createElement("config");
this.doc.appendChild(this.configroot);
BaseUser admin = new BaseUser();
admin.setUserName("ybbkd2");
admin.setPassword("721521");
admin.setEmail("[email protected]");
if (!this.CreateUser(admin)) {
throw new Exception("001 配置文件不存在,重新建立时出现错误 ,管理员帐号建立错误.");
}
}
}
public boolean CreateUser (BaseUser user) {
Element newuser = doc.createElement("user");
newuser.setAttribute("username",user.getUserName());
newuser.setAttribute("password",user.getPassword());
newuser.setAttribute("email",user.getEmail());
//保存建立时间
Calendar now = Calendar.getInstance();
Date date = now.getTime();
DateFormat df = DateFormat.getDateInstance();
String sdate = df.format(date);
newuser.setAttribute("createdate",sdate);
Element newgb = doc.createElement("guestbook");
newgb.setAttribute("current-id","1");
Element newdoc = doc.createElement("document");
newdoc.setAttribute("current-id","1");
//构建树
this.configroot.appendChild(newuser);
newuser.appendChild(newgb);
newuser.appendChild(newdoc);
//保存到磁盘,失败 return false
try {
((XmlDocument)doc).write(new FileOutputStream(cfg));
} catch (IOException e) {
return false;
}
return true;
}
}
运行几次之后XML为:<?xml version="1.0" encoding="UTF-8"?><!--站点设置-->
<config>
<user username="ybbkd2" password="721521" email="[email protected]" createdate="2005-7-2">
<guestbook current-id="1" />
<document current-id="1" />
</user>
<user username="hotmail" password="123" email="[email protected]" createdate="2005-7-2">
<guestbook current-id="1" />
<document current-id="1" />
</user>
<user username="hotmail" password="123" email="[email protected]" createdate="2005-7-2">
<guestbook current-id="1" />
<document current-id="1" />
</user>
<user username="hotmail" password="123" email="[email protected]" createdate="2005-7-2">
<guestbook current-id="1" />
<document current-id="1" />
</user>
<user username="hotmail" password="123" email="[email protected]" createdate="2005-7-2">
<guestbook current-id="1" />
<document current-id="1" />
</user>
<user username="hotmail" password="123" email="[email protected]" createdate="2005-7-2">
<guestbook current-id="1" />
<document current-id="1" />
</user> <user username="hotmail" password="123" email="[email protected]" createdate="2005-7-7">
<guestbook current-id="1" />
<document current-id="1" />
</user>
</config>只要添加一条记录空行就会增大一行,请问大家有什么好方法么?
SiteUserConfig cfg=null;
try {
cfg = new SiteUserConfig();
} catch (Exception e) {
System.out.println(e.getMessage());
return;
}
BaseUser user = new BaseUser();
user.setEmail("[email protected]");
user.setPassword("123");
user.setUserName("hotmail");
cfg.CreateUser(user);
}
}
newuser.appendChild(newgb);
newuser.appendChild(newdoc);
这里我觉得有问题,按下面改改试试: newuser.appendChild(newgb);
newuser.appendChild(newdoc);
this.configroot.appendChild(newuser);
我是做xml文件的查询。然后如果文件中没有查询的数据,则添加一行新的数据。也出现了和个问题。
我只好使用javabean的方法解决了。
之类的boolean型或枚举型参数?我记得 jdom 写出的时候有个参数 表示 要 自动缩进 格式 。
不过不知道会不会多出空行来。
this.configroot.appendChild(newuser);
newuser.appendChild(newgb);
newuser.appendChild(newdoc);
这里我觉得有问题,按下面改改试试:newuser.appendChild(newgb);
newuser.appendChild(newdoc);
this.configroot.appendChild(newuser);此处逻辑上有问题,你又得到了结果说明其他地方逻辑上也会有问题。你要自己查查程序了,这里只能给个提示,问题还要自己去解决的。