
已解决问题
谷歌fjmcl888用户在2013.09.10提交了关于“叶圣陶请问这个怎么错了?为什么在写入的时候 提示前言不允有内容”的提问,欢迎大家涌跃发表自己的观点。目前共有1个回答,最后更新于2024-12-15T20:05:38。<?xmlversion="1.0"encoding="UTF-8"standalone="no"?><novellist>
<novelname="天珠变"intro="12天珠,大闹玄天!"author="唐家三少"type="玄幻"address="E:/Users/workspace/U1/xh/天珠变.txt"></novel>
<novelname="精武门"intro="弱肉强食、适者生存!"author="格斗家"type="武侠"address="E:/Users/workspace/U1/武侠/精武门.txt"></novel>
</novellist>
用的是dom解析
publicvoidwrite(Documentdoc,Stringaddress){
try{
//写入公**类
TransformerFactorytff=TransformerFactory.newInstance();
Transformertf=tff.newTransformer();
DOMSourcedom=newDOMSource(doc);
tf.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
StreamResultsr=newStreamResult(newFileOutputStream(address));
tf.transform(dom,sr);
}catch(Exceptione){
System.out.println(e.getMessage());
}
}
//写入
publicbooleanuploadtxt(StringnovelName,StringnovelIntro,StringnovelAuthor,StringnovelType,StringnovelAddress){
Documentdoc=getDocument(Tools.getValue(Constans.SYS_UPLOAD));
//服务器路径
Stringupload=**ll;
Stringtype=novelType;
Stringname=novelName;
Stringauthor=novelAuthor;
Stringintro=novelIntro;
upload=Tools.getValue(Constans.SYS_UPLOAD)+type+"//"+name+".txt";
//novel元素节点
ElementnoveElement=doc.createElement("novel");
//节点内属性值
noveElement.setAttribute("name",name);
noveElement.setAttribute("intro",intro);
noveElement.setAttribute("author",author);
noveElement.setAttribute("type",type);
noveElement.setAttribute("address",upload);
//根节点
Elementnovellist=(Element)doc.getElementsByTagName("novellist").item(0);
//父子关系
novellist.appendChild(noveElement);
//将各值写入XML
write(doc,Tools.getValue(Constans.SYS_NOVELXML));希望大家能够帮助她。
详细问题描述及疑问:<?xmlversion="1.0"encoding="UTF-8"standalone="no"?>
<novellist>
<novelname="天珠变"intro="12天珠,大闹玄天!"author="唐家三少"type="玄幻"address="E:/Users/workspace/U1/xh/天珠变.txt"></novel>
<novelname="精武门"intro="弱肉强食、适者生存!"author="格斗家"type="武侠"address="E:/Users/workspace/U1/武侠/精武门.txt"></novel>
</novellist>
用的是dom解析
publicvoidwrite(Documentdoc,Stringaddress){
try{
//写入公**类
TransformerFactorytff=TransformerFactory.newInstance();
Transformertf=tff.newTransformer();
DOMSourcedom=newDOMSource(doc);
tf.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
StreamResultsr=newStreamResult(newFileOutputStream(address));
tf.transform(dom,sr);
}catch(Exceptione){
System.out.println(e.getMessage());
}
}
//写入
publicbooleanuploadtxt(StringnovelName,StringnovelIntro,StringnovelAuthor,StringnovelType,StringnovelAddress){
Documentdoc=getDocument(Tools.getValue(Constans.SYS_UPLOAD));
//服务器路径
Stringupload=**ll;
Stringtype=novelType;
Stringname=novelName;
Stringauthor=novelAuthor;
Stringintro=novelIntro;
upload=Tools.getValue(Constans.SYS_UPLOAD)+type+"//"+name+".txt";
//novel元素节点
ElementnoveElement=doc.createElement("novel");
//节点内属性值
noveElement.setAttribute("name",name);
noveElement.setAttribute("intro",intro);
noveElement.setAttribute("author",author);
noveElement.setAttribute("type",type);
noveElement.setAttribute("address",upload);
//根节点
Elementnovellist=(Element)doc.getElementsByTagName("novellist").item(0);
//父子关系
novellist.appendChild(noveElement);
//将各值写入XML
write(doc,Tools.getValue(Constans.SYS_NOVELXML));期待您的答案,千言万语,表达不了我的感激之情,我已铭记在心
!