Package com.softsynth.jmsl.util
Class SimpleXMLLoader
java.lang.Object
com.softsynth.jmsl.util.SimpleXMLLoader
- All Implemented Interfaces:
XMLListener
- Direct Known Subclasses:
InstrumentXMLLoader
public class SimpleXMLLoader extends java.lang.Object implements XMLListener
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)
- Author:
- Nick Didkovsky, (c) 2001 Nick Didkovsky all rights reserved
-
Constructor Summary
Constructors Constructor Description SimpleXMLLoader() -
Method Summary
Modifier and Type Method Description voidbeginElement(java.lang.String tag, java.util.Hashtable attributes, boolean ifEmpty)Handles the start of an element.voidendElement(java.lang.String tag)Handles the end of an element.voidfoundContent(java.lang.String content)Handles the content of an element.java.lang.ObjectloadXML(java.io.InputStream stream)java.lang.ObjectloadXML(java.lang.String fileName)static voidmain(java.lang.String[] args)voidsetObjectListener(NewXMLObjectListener listener)Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Constructor Details
-
SimpleXMLLoader
public SimpleXMLLoader()
-
-
Method Details
-
loadXML
public final java.lang.Object loadXML(java.io.InputStream stream) throws java.io.IOException- Throws:
java.io.IOException
-
loadXML
public java.lang.Object loadXML(java.lang.String fileName) throws java.io.IOException- Throws:
java.io.IOException
-
setObjectListener
-
beginElement
public void beginElement(java.lang.String tag, java.util.Hashtable attributes, boolean ifEmpty) throws XMLLoaderExceptionDescription copied from interface:XMLListenerHandles the start of an element. The flag ifEmpty if there is no content or endTag.- Specified by:
beginElementin interfaceXMLListener- Throws:
XMLLoaderException
-
foundContent
public void foundContent(java.lang.String content)Description copied from interface:XMLListenerHandles the content of an element.- Specified by:
foundContentin interfaceXMLListener
-
endElement
public void endElement(java.lang.String tag)Description copied from interface:XMLListenerHandles the end of an element.- Specified by:
endElementin interfaceXMLListener
-
main
public static void main(java.lang.String[] args)
-