Converting XML to Java objects [closed]

If you have an XML schema , JAXB is nice – comes as part of the JDK. Generate java classes by running e.g. xjc -p foo myschema.xsd

To read an XML file and get back an object (from classes generated by the xjc tool):

    JAXBContext context = JAXBContext.newInstance(FooObj.class);
    Unmarshaller unMarshaller = context.createUnmarshaller();
    FooObj param = (FooObj) unMarshaller.unmarshal(new FileInputStream("Foo.xml"));

You can do similar things if you only want parts of an XML document converted to an object, you should e.g. be able to give JAXB part of a DOM document, instead of a whole file as done above.

Leave a Comment