We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › How to write a XML file
Page Index Toggle Pages: 1
How to write a XML file? (Read 870 times)
How to write a XML file?
Jun 27th, 2009, 1:37pm
 
Hi,
I am doing a cellular automata analysis and like to store my data
into a XMLfile for further use.
I thought it was a good idea to store the data inside XMLElement.
Now I like to save the XML to a file. I discovererd the XMLWriter class inside the developers api documentation...
but how does it work?

Here is my poor try:


XMLElement fieldofbasins= new XMLElement();
fieldofbasins.setName("FieldOfBasins");
fieldofbasins.setAttribute( "Parameter","0815");
fieldofbasins.setContent("inhalt");

// file schreiben
PrintWriter xmlfile;
String feld = "HalloWelt";
xmlfile = createWriter(feld+".xml");

println(fieldofbasins.getName());
print(fieldofbasins.getContent());

//xml schreiben
XMLWriter schreibXML = new XMLWriter(xmlfile) ;

schreibXML.write(fieldofbasins);

xmlfile.flush();
xmlfile.close();


Perhaps somebody could help, plz

thx
Alexander
Re: How to write a XML file?
Reply #1 - Jun 28th, 2009, 12:38am
 
Unhandled exception means you need to add a try/catch block around the code needing it.
Processing insulates most of its API from these exceptions (sometime too much, I think) but not there.
Code you must change:
Code:
//xml schreiben
try
{
XMLWriter schreibXML = new XMLWriter(xmlfile) ;

schreibXML.write(fieldofbasins);

xmlfile.flush();
xmlfile.close();
}
catch (IOException e)
{
e.printStackTrace();
}
Page Index Toggle Pages: 1