XML-Datei erzeugen

[code lang=“java“]import org.w3c.dom.Document;
import com.sun.org.apache.xerces.internal.dom.DocumentImpl;
import org.w3c.dom.Element;
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;

Document doc = new DocumentImpl();
Element root = doc.createElement(„xTree“);
doc.appendChild(root);

Element e = doc.createElement(„xTreeFile“);
e.setAttribute(„label“, „label1“);
e.setAttribute(„type“, „type1“);
e.setTextContent(„abc“);

root.appendChild(e);

OutputFormat format = new OutputFormat(doc);
format.setIndenting(true);
format.setIndent(4);
String[] str = new String[]{„xTreeText“};
format.setCDataElements(str);
format.setNonEscapingElements(str);
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(„C:/Temp/test.xml“), „UTF-8“);
Writer output = new BufferedWriter(out);
XMLSerializer serializer = new XMLSerializer(output, format);
serializer.serialize(doc);[/code]