JAXB preguntas y respuestas de entrevista
Pregunta 11. Java code for marshalling Java objects into XML.
β’ Example
Marshaller m = factory.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
Writer fw = new FileWriter("newCars.xml");
m.marshal(cars, fw);
β’ marshal method accepts
β java.io.OutputStream
β java.io.Writer
β javax.xml.transform.Result
β’ related to XSLT
β org.w3c.dom.Node
β’ related to DOM
β org.xml.sax.ContentHandler
β’ related to SAX
β’ Other Marshaller methods
β boolean setEventHandler(ValidationEventHandler handler)
β’ same as use with Unmarshaller, but validation events
are delivered during marshalling
β void setProperty(String name, Object value)
β’ supported properties are
β jaxb.encoding - value is a String
Β» the encoding to use when marshalling; defaults to βUTF-8β
β jaxb.formatted.output - value is a Boolean
Β» true to output line breaks and indentation; false to omit (the default)
β jaxb.schemaLocation - value is a String
Β» to specify xsi:schemaLocation attribute in generated XML
β jaxb.noNamespaceSchemaLocation - value is a String
Β» to specify xsi:noNamespaceSchemaLocation attribute in generated XML
Pregunta 12. Java example/java program to set object for generating XML.
Cars cars = factory.createCars();
Car car = factory.createCar();
car.setColor("blue");
car.setMake("Mazda");
car.setModel("Miata");
car.setYear(BigInteger.valueOf(2012));
cars.getCar().add(car);
car = factory.createCar();
car.setColor("red");
car.setMake("Ford");
car.setModel("Mustang II");
car.setYear(BigInteger.valueOf(2011));
cars.getCar().add(car);
Pregunta 13. How to validate java objects?
β’ The graph of Java objects can contain invalid data
β could occur when objects created by unmarshalling are modified
β could occur when objects are created from scratch
β’ Use a Validator to validate the objects
β’ Example
Validator v = factory.createValidator();
try {
v.validateRoot(cars);
v.validate(car);
} catch (ValidationException e) {
// Handle the validation error described by e.getMessage().
}
β’ Other Validator methods
β boolean setEventHandler(ValidationEventHandler handler)
β’ handleEvent method of ValidationEventHandler is called
if validation errors are encountered
β’ default handler terminates marshalling after first error
β’ return true to continue validating
β’ return false to terminate with ValidationException
Pass an instance of javax.xml.bind.util.ValidationEventCollector
(in jaxb-api.jar) to setEventHandler to collect validation errors and
query them later instead of handling them during validation.
ValidationEventCollector vec =
new ValidationEventCollector();
v.setEventHandler(vec);
v.validate(cars);
ValidationEvent[] events = vec.getEvents();
Pregunta 14. Customizing Type Bindings.
β’ Default bindings can be overridden
β at global scope
β on case-by-case basis
β’ Customizations include
β names of generated package, classes and methods
β method return types
β class property (field) types
β which elements are bound to classes, as opposed to being ignored
β class property to which each attribute and element declaration is bound
Pregunta 15. Syntax of customization.
β’ Customizations can be specified in
β the XML Schema (our focus)
β a binding declarations XML document (not well supported by RI yet)
β’ The XML Schema must declare
the JAXB namespace and version
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchemaβ
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
jxb:version="1.0">
β’ Customization elements are placed in annotation elements
<xsd:annotation>
<xsd:appinfo>
binding declarations
</xsd:appinfo>
</xsd:annotation>
Lo mas util segun los usuarios:
- What Is XML Binding?
- What is XML Binding Relationships?
- Please explain JAXB Use Cases.
- Why Use XML Binding?
- What are the goals of JAXB?