com.softsynth.jmsl.util
Class SimpleXMLSaver
java.lang.Object
com.softsynth.jmsl.util.SimpleXMLSaver
public class SimpleXMLSaver
- extends java.lang.Object
Write out an XML file that describes an Object, with values for all matching get/set methods that
handle primitive types (String, boolean, int, double), the class name, and tag of your choice.
Example, Class com.widgits.City below has matching get/set methods for popuation
package com.widgits;
public class City {
int population;
public City() {
}
public void setPopulation(int p) {
this.population = p;
}
public int getPopulation() {
return population;
}
public static void main(String args[]) {
try {
City myCity = new City();
myCity.setPopulation(10000);
java.io.PrintWriter pout = new java.io.PrintWriter(new java.io.FileOutputStream("city.xml"));
(new com.softsynth.jmsl.util.SimpleXMLSaver(myCity, "SomeXMLTag")).writeXML(pout);
pout.close();
// now read it back in to create a new object with same population as the saved one
com.softsynth.jmsl.util.SimpleXMLLoader loader = new com.softsynth.jmsl.score.util.SimpleXMLLoader();
City loadedCity= (City)loader.loadXML("city.xml");
System.out.println("population=" + loadedCity.getPopulation());
}
catch (java.io.IOException e) {
System.out.println("ERROR in main: " + e);
}
}
}
The XML file written in main() looks like this:
(openbracket)SomeXMLTag CLASSNAME="com.widgits.City" Population="10000" (closebracket)
(openbracket)/SomeXMLTag (closebracket)
- Author:
- Nick Didkovsky, (c) 2001 Nick Didkovsky all rights reserved
Constructor Summary |
SimpleXMLSaver(java.lang.Object obj,
java.lang.String tag)
|
Method Summary |
static void |
main(java.lang.String[] args)
|
void |
writeXML(java.io.PrintWriter out)
XMLWritable interface |
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
SimpleXMLSaver
public SimpleXMLSaver(java.lang.Object obj,
java.lang.String tag)
throws java.io.IOException
- Throws:
java.io.IOException
writeXML
public void writeXML(java.io.PrintWriter out)
throws java.io.IOException
- XMLWritable interface
- Throws:
java.io.IOException
main
public static void main(java.lang.String[] args)