[英]Writing from Java to an XML document - Simple
我知道在stackoverflow上從Java寫到XML有很多問題,但它太復雜了。 我覺得我有一個非常簡單的問題,我無法弄明白。
所以我有一個程序需要大量的用戶輸入,我現在正在創建並附加帶有結果的文本文檔。 我將在這里發布我的編寫器代碼:
PrintWriter out = null;
try {
out = new PrintWriter(new BufferedWriter(new FileWriter("C:/Documents and Settings/blank/My Documents/test/test.txt", true)));
out.println("");
out.println("<event title=\""+titleFieldUI+"\"");
out.println(" start=\""+monthLongUI+" "+dayLongUI+" "+yearLongUI+" 00:00:00 EST"+"\"");
out.println(" isDuration=\"true\"");
out.println(" color=\""+sValue+"\"");
out.println(" end=\""+monthLong1UI+" "+dayLong1UI+" "+yearLong1UI+" 00:00:00 EST"+"\"");
out.println(" "+descriptionUI);
out.println("");
out.println("</event>");
out.println(" <!-- Above event added by: " +System.getProperty("user.name")+" " +
"on: "+month+"/"+day+"/"+year+" -->");
}catch (IOException e) {
System.err.println(e);
}finally{
if(out != null){
out.close();
}
}
所以最后,我希望它寫入一個已經存在的XML文件(我可以通過簡單地改變我的編寫器所在的位置來完成)。 問題是,這個XML文件有一個稱為<data>
根標記。 我需要將我的程序的結果放在XML文件的底部,但是請先進行</data>
。 這是唯一的要求。 我發現的一切看起來都太復雜了,我無法弄明白......
很感謝任何形式的幫助!
您應該使用一個不錯的XML API。 例如,這是使用JDOM的示例:
import java.io.*;
import org.jdom2.*;
import org.jdom2.input.*;
import org.jdom2.output.*;
public class Test {
public static void main(String args[]) throws IOException, JDOMException {
File input = new File("input.xml");
Document document = new SAXBuilder().build(input);
Element element = new Element("event");
element.setAttribute("title", "foo");
// etc...
document.getRootElement().addContent(element);
// Java 7 try-with-resources statement; use a try/finally
// block to close the output stream if you're not using Java 7
try(OutputStream out = new FileOutputStream("output.xml")) {
new XMLOutputter().output(document, out);
}
}
}
這真的不是那么難......而且它比手動寫出來要強大得多。 (例如,如果您的事件標題包含“&”,這將做正確的事情 - 而您的代碼會產生無效的XML。)
File file = new File("projects.xml");
Document document = $(file).document();
Comment eventComment = document.createComment("Above event added by: "
+ System.getProperty("user.name") + "\n" +
" on: " + month + "/" + day + "/" + year);
document = $(file)
.xpath("//data")
.append($("event",
$("title", "titleFieldUI"),
$("start", monthLongUI + " " + dayLongUI + " " + yearLongUI + " 00:00:00 EST"),
$("isDuration", "true"),
$("color", sValue),
$("end", monthLong1UI + " " + dayLong1UI + " " + yearLong1UI + " 00:00:00 EST")))
.append($(eventComment))
.document();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
Result output = new StreamResult(file);
Source input = new DOMSource(document);
transformer.transform(input, output);
XMLBuilder builder = XMLBuilder.parse(
new InputSource(new FileReader("C:/Documents and Settings/blank/My Documents/test/test.txt")))
.xpathFind("//data")
.e("event")
.a("title", titleFieldUI)
.a("start", monthLongUI + " " + dayLongUI + " " + yearLongUI + " 00:00:00 EST")
.a("isDuration", "true")
.a("color", sValue)
.a("end", monthLong1UI + " " + dayLong1UI + " " + yearLong1UI + " 00:00:00 EST")
.up()
.comment("Above event added by: " + System.getProperty("user.name") + "\n" +
" on: " + month + "/" + day + "/" + year);
PrintWriter writer = new PrintWriter(new FileOutputStream("C:/Documents and Settings/blank/My Documents/test/test.txt"));
builder.toWriter(writer, new Properties());
你可以使用JOOX 。 這是你如何做一個追加:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = factory.newDocumentBuilder();
Document doc = parser.parse("/pathToXML");
JOOX.$(doc).append("<newNode>data<newNode>");
默認情況下,$(doc)將加載根節點。 如果需要內部節點,可以使用find()方法。 圖書館沒有很好的文檔記錄,但作為開源,您可以隨時直接檢查源。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.