Writing XML documents from Java – simple

I know there are many problems in writing XML from Java on stack overflow, but it is too complex I think I have a very simple question that I can't understand

So I have a program that requires a lot of user input, and I'm now creating and attaching a text document with results I will publish my writer code here:

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();
             }
         }

So finally, I want it to write to an existing XML file (I can do this by simply changing the location of my writer) The problem is that the XML file has a root tag called < data > I need to put the results of my program at the bottom of the XML file, but please come before < / Data > This is the only requirement Everything I found seemed too complicated for me to understand

Thank you for any help!

Solution

You should use a good XML API For example, this is an example of using 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);
        }
    }
}

It's really not that hard... And it's much more powerful than writing it out manually (for example, if your event title contains "&", it will do the right thing – and your code will produce invalid XML.)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>