JAXB 면접 질문과 답변
Ques 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
Ques 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);
Ques 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();
Ques 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
Ques 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>
Most helpful rated by users:
- What Is XML Binding?
- What is XML Binding Relationships?
- Please explain JAXB Use Cases.
- Why Use XML Binding?
- What are the goals of JAXB?