import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* Datei im XML-Format (*.xml) speichern
* @param filename Dateiname
* @return true, wenn Speichern erfolgreich
*/
public boolean SaveXML(String filename)
{
boolean bRetVal = false;
try
{
File f = new File(filename);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
// root
Document doc = builder.newDocument();
Element root = doc.createElement("root");
doc.appendChild(root);
// <value>1.0</value>
Element v = doc.createElement("value");
v.appendChild(doc.createTextNode("1.0"));
root.appendChild(v);
for (int i = 0; i < 10; i++)
{
// <type value="0"/>
Element t = doc.createElement("type");
t.setAttribute("value", Integer.toString(i));
root.appendChild(t);
for (int j = 0; j < 10; j++)
{
// <type_value v1="0" v2="0"/>
Element tv = doc.createElement("type_value");
tv.setAttribute("v1", Integer.toString(i+j));
tv.setAttribute("v2", Integer.toString(i*j));
raster.appendChild(tv);
}
//</type>
}
// write xml
Transformer transformer = TransformerFactory.newInstance().newTransformer();
//transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
//transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(new DOMSource(doc), new StreamResult(f));
bRetVal = true;
}
catch (Exception e)
{
bRetVal = false;
}
finally
{
}
return bRetVal;
}