没有问题的,给分吧~~
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();
}
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();
}
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);
}
jdom也一样的,XMLOutputter(java.lang.String indent, boolean newlines)
XMLOutputter(" ",true);
It's very useful!