This makes it easy to read in different types of documents without knowing their type in advance. JAXBContext jaxb Context = Instance(package Name); Unmarshaller unmarshaller = jaxb Context.create Unmarshaller(); Validating(true); Item item = (Item) unmarshaller.unmarshal(new File("item.xml")); ...
JAXBContext jc = Instance( "foo" ); Unmarshaller u = jc.create Unmarshaller(); Document Builder Factory dbf = Document Builder Instance(); Namespace Aware(true); Document Builder db = Document Builder(); Document doc = db.parse(new File( "xml File.xml")); Object o = u.unmarshal( doc ); can validate a document with the schema while unmarshalling.
JAXB defines an architecture for binding XML schemata to Java objects.
This allows client apps to check if a graph is okay and ready to process; if not, validation will identify objects that contain errors so that, for example, client apps can ask users to fix those. JAXBContext jaxb Context = Instance(package Name); Object Factory item Maker = new Object Factory(); Item item = item Maker.create Item(); Validator validator = jaxb Context.create Validator(); if(! But doing so makes it much harder to associate errors with their sources, which makes debugging harder for humans.These three aspects of JAXB are covered by three separate interfaces.Instances of those interfaces can be created from a object so that it is capable of managing the schema derived interfaces.As with XSLT transformation explained in the previous volume, we can easily perform this check if we use the standard API "JAXP (Java API for XML Processing)" included in J2SE 5.0.List1 shows an example of a program created in JAXP that performs a validity check.