用DOM生成XML文件,如何回车换行??
我用DOM生成的XML文件全在一行,如何才能换行?? 问题点数:50、回复次数:9Top
1 楼gengxy(老傻瓜)回复于 2003-04-08 10:12:26 得分 0
有人知道吗!Top
2 楼beyondhyb(浪子)回复于 2003-05-29 12:55:42 得分 0
关注Top
3 楼yy23rock(云云)回复于 2003-05-29 13:01:47 得分 35
没有问题的,给分吧~~
try
{
//´´½¨ÐµÄxmlÎļþ
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document documentBase = builder.newDocument();
//´´½¨¸ù½áµã
Element eTopic = documentBase.createElement("Topic");
documentBase.appendChild(eTopic);
//´´½¨·¢ÐÂÌù½áµã²¢Ð´ÈëÐÂÌùÐÅÏ¢
Element eIssue=documentBase.createElement("Issue");
eTopic.appendChild(eIssue);
//´´½¨×Ó½áµãдÈëÐÅÏ¢
Element eName = documentBase.createElement("Name");
Text tValue = documentBase.createTextNode("");
tValue.setNodeValue(vContext.get(0).toString());
eName.appendChild(tValue);
eIssue.appendChild(eName);
Element eCaption = documentBase.createElement("Caption");
tValue = documentBase.createTextNode("");
tValue.setNodeValue(vContext.get(1).toString());
eCaption.appendChild(tValue);
eIssue.appendChild(eCaption);
Element eContext = documentBase.createElement("Content");
tValue = documentBase.createTextNode("");
tValue.setNodeValue(vContext.get(2).toString());
eContext.appendChild(tValue);
eIssue.appendChild(eContext);
Element eTime = documentBase.createElement("Time");
tValue = documentBase.createTextNode("");
tValue.setNodeValue(vContext.get(3).toString());
eTime.appendChild(tValue);
eIssue.appendChild(eTime);
//´´½¨»Ø¸´½áµã£¨¿Õ£©
Element eAllReplys=documentBase.createElement("AllReplys");
eTopic.appendChild(eAllReplys);
//дÎļþ
PrintWriter pWriter=new PrintWriter(new java.io.FileOutputStream(strPath));
XMLSerializer serl=new XMLSerializer(pWriter,new OutputFormat("xml","gb2312",true));//这里的参数(TRUE)表示格式化为自动换行
serl.processingInstruction("xml:stylesheet","type=\"text/xsl\" href=\"bbs.xsl\"");
serl.serialize(documentBase);
pWriter.close();
}
catch(Exception e)
{
strLastErr="err: " + e.getMessage().toString();
}
Top
4 楼mymoto(忽忽)回复于 2003-05-29 13:17:20 得分 0
晕,有没有jdom的,w3c的没用过Top
5 楼beyondhyb(浪子)回复于 2003-05-29 13:37:53 得分 0
XMLSerializer是哪个包里的Top
6 楼beyondhyb(浪子)回复于 2003-05-29 13:43:27 得分 0
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
Properties properties = t.getOutputProperties();
properties.setProperty(OutputKeys.ENCODING, "Shift_JIS");
t.setOutputProperties(properties);
t.transform(doms, sr);
} catch (TransformerConfigurationException ex) {
throw new Exception("ドキュメントオブジェクトを該当のファイルに格納する場合、エラーが出来る", ex);
} catch (TransformerException ex) {
throw new Exception(
"ドキュメントオブジェクトを該当のファイルに格納する場合、エラーが出来る",
ex);
} catch (Exception ex) {
throw new Exception(
"ドキュメントオブジェクトを該当のファイルに格納する場合、エラーが出来る",
ex);
}Top
7 楼beyondhyb(浪子)回复于 2003-06-02 09:10:06 得分 0
关注
Top
8 楼Debian(乌鱼子)回复于 2003-06-02 12:50:27 得分 15
to:mymoto(忽忽)
jdom也一样的,XMLOutputter(java.lang.String indent, boolean newlines)
XMLOutputter(" ",true);
Top
9 楼dingweiqing(ddd)回复于 2003-07-30 12:01:33 得分 0
thanks a lot!
It's very useful!Top




