|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.softsynth.jmsl.util.SimpleXMLLoader
public class SimpleXMLLoader
Read in an XML file that describes an Object, and recreate the object with values found for all matching get/set methods that handle primitive types (String, boolean, int, double), 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 {
// create a new City and write it out as an XML file
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.score.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.score.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)
| Constructor Summary | |
|---|---|
SimpleXMLLoader()
|
|
| Method Summary | |
|---|---|
void |
beginElement(java.lang.String tag,
java.util.Hashtable attributes,
boolean ifEmpty)
Handles the start of an element. |
void |
endElement(java.lang.String tag)
Handles the end of an element. |
void |
foundContent(java.lang.String content)
Handles the content of an element. |
java.lang.Object |
loadXML(java.io.InputStream stream)
|
java.lang.Object |
loadXML(java.lang.String fileName)
|
static void |
main(java.lang.String[] args)
|
void |
setObjectListener(NewXMLObjectListener listener)
|
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public SimpleXMLLoader()
| Method Detail |
|---|
public java.lang.Object loadXML(java.io.InputStream stream)
throws java.io.IOException
java.io.IOException
public java.lang.Object loadXML(java.lang.String fileName)
throws java.io.IOException
java.io.IOExceptionpublic void setObjectListener(NewXMLObjectListener listener)
public void beginElement(java.lang.String tag,
java.util.Hashtable attributes,
boolean ifEmpty)
throws XMLLoaderException
XMLListener
beginElement in interface XMLListenerXMLLoaderExceptionpublic void foundContent(java.lang.String content)
XMLListener
foundContent in interface XMLListenerpublic void endElement(java.lang.String tag)
XMLListener
endElement in interface XMLListenerpublic static void main(java.lang.String[] args)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||