日期:2014-05-19  浏览次数:20668 次

为什么dom保存的时候把dtd给漏了?
保存前
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE otherset PUBLIC "hengsheng" "OtherSet.dtd">
<otherset>
程序配置设置
<setprogram>
<userpswd>
<username>nbcx</username>
<password>nbcxyey</password>
</userpswd>
</setprogram>
</otherset>
程序代码
 DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
Document doc= db.parse(new File("OtherSet.xml"));  
Element eltupd= (Element) doc.getElementsByTagName("userpswd").item(0);
Node nduser=eltupd.getElementsByTagName("username").item(0);
nduser.getFirstChild().setNodeValue(text.getText());  
TransformerFactory tff=TransformerFactory.newInstance();
Transformer tf=tff.newTransformer();
tf.setOutputProperty("encoding","UTF-8");
DOMSource source=new DOMSource(doc);
StreamResult result=new StreamResult(new File("OtherSet.xml"));
tf.transform(source,result);
结果修改后进行保存,xml中
<!DOCTYPE otherset PUBLIC "hengsheng" "OtherSet.dtd">这一句就没有了.

------解决方案--------------------
我知道了,使用dom是不行的,必须换jdom或dom4j