Download Android App in your Android Device from Google Play Store
- Search for "Withoutbook Practice Exam Test" in Mobile/Tablet Play Store
Practice InterviewNew Search by Name or Email

Exams Attended

Make Homepage

Bookmark this page

Subscribe Email Address

JAXB Interview Questions and Answers

Ques 11. Java code for marshalling Java objects into XML.

Ans. • 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
– 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

Is it helpful? Add Comment View Comments
Ques 12. Java example/java program to set object for generating XML.
Ans. Cars cars = factory.createCars();
Car car = factory.createCar();
car = factory.createCar();
car.setModel("Mustang II");
Is it helpful? Add Comment View Comments
Ques 13. How to validate java objects?
Ans. • 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 {
} 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();
ValidationEvent[] events = vec.getEvents();
Is it helpful? Add Comment View Comments
Ques 14. Customizing Type Bindings.
Ans. • 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
Is it helpful? Add Comment View Comments
Ques 15. Syntax of customization.
Ans. • 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="”
• Customization elements are placed in annotation elements
binding declarations
Is it helpful? Add Comment View Comments

Most helpful rated by users:

©2020 WithoutBook