Apache JAXB Java XML Java Transformations
From Kb
Contact Article Author | Blog of Article Author | FirstPartners.net Home | LinkedIn profile of Author
See Also
- Apache JAXB Java XML Java Transformations
- Castor Java XML Java Mapping
- XStream Java XML Java Mapping
Sample Code
import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; private MyObject loadObjectFromFile() throws JAXBException{ JAXBElement o=null; File inputXml = new File(MY_FILE_NAME); //Note that the package and mappings need compiled with the JAXB compiler first JAXBContext context = JAXBContext.newInstance("net.firstpartners.mypackage"); try{ InputStream inXmlStream = new FileInputStream(inputXml); Unmarshaller unmarshaller = context.createUnmarshaller(); o = (JAXBElement) unmarshaller.unmarshal(inXmlStream); } catch (FileNotFoundException fnfe){ return null; } return (MyObject) o.getValue(); } private void saveObjectToXmlFile(MyObject object) throws FileNotFoundException , JAXBException{ FileOutputStream fileOutput = new FileOutputStream( new File(MY_FILE_NAME)); JAXBContext jaxbContext = JAXBContext.newInstance("net.firstpartners.mypackage"); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(outClaimSet, fileOutput); }

