Forget writing XML parser...Castor will take care of parsing. It generates the java
classes to work with XML.
To use castor,
-- put the below lib's in classpath
-- ./lib/castor-0.9.9.jar
./lib/castor-0.9.9-srcgen-ant-task.jar
./lib/castor-0.9.9-xml.jar
xercesImpl.jar;
commons-logging.jar
-- Run
java org.exolab.castor.builder.SourceGenerator -i test.xsd -dest dtnFolder
--optionally specify the package name if required.
-- To convert from DTD to XSD use
java org.exolab.castor.xml.dtd.Converter dtdFileName newXSDFileName
To use the generated code:
- To run the unmarshal the castor lib should be in class path and follow the sample code herewith.
// Create a Reader to the file to unmarshal from
reader = new FileReader("test.xml");
// Marshal the person object
Person person = (Person)Unmarshaller.unmarshal(Person.class, reader);
- To marshal an instance of the person class you simply call the Marshaller as follows:
// Create a new Person
Person person = new Person("Ryan 'Mad Dog' Madden");
person.setDateOfBirth(new Date(1955, 8, 15));
// Create a File to marshal to
writer = new FileWriter("test.xml");
// Marshal the person object
Marshaller.marshal(person, writer);
Follow the document of castor for details.
No comments:
Post a Comment