Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

JAXB Interview Questions and Answers

Ques 6. What are the disadvantages/non-goals of JAXB?

• Standardize generated Java
– classes generated by different
JAXB implementations may not be
compatible with each other
• Preserve XML equivalence
– unmarshalling XML to objects and
marshalling back to XML may not
result in equivalent XML
• Bind existing JavaBeans
to schemas
– can only marshal and unmarshal
classes generated by JAXB
– may be added later
• Schema evolution support
– can’ t modify previously generated
code to support schema changes
– must generated new code
• Allow generated Java to access
XML elements/attributes not
described in initial schema
• Partial binding
– unmarshalling only a subset
of an XML document
breaks round tripping
• Implement every feature of the
schema language
– it’ s tough to implement
all of XML Schema!
• Support DTDs
– focusing on XML Schema
– DTDs were supported in an earlier
version, but won’ t be anymore
– tools for converting DTDs to
XML Schemas exist

Is it helpful? Add Comment View Comments
 

Ques 7. Give me an example of XSD.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.withoutbook.com/cars"
targetNamespace="http://www.withoutbook.com/cars">
<xs:complexType name="car">
<xs:sequence>
<xs:element name="make" type="xs:string"/>
<xs:element name="model" type="xs:string"/>
<xs:element name="color" type="xs:string"/>
</xs:sequence>
<xs:attribute name="year" type="xs:positiveInteger" use="required"/>
</xs:complexType>
<xs:element name="cars">
<xs:complexType>
<xs:sequence>
<xs:element name="car" type="car" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Is it helpful? Add Comment View Comments
 

Ques 8. Give me an example of XML document.

<?xml version="1.0" encoding="UTF-8"?>
<cars xmlns="http://www.withoutbook.com/cars"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.withoutbook.com/cars cars.xsd">
<car year="2001">
<make>BMW</make>
<model>Z3</model>
<color>yellow</color>
</car>
<car year="2001">
<make>Honda</make>
<model>Odyssey</model>
<color>green</color>
</car>
<car year="1997">
<make>Saturn</make>
<model>SC2</model>
<color>purple</color>
</car>
</cars>

Is it helpful? Add Comment View Comments
 

Ques 9. How to generate Java from XML Schema. Please show the example.

• From command-line
– Windows: %JAXB_HOME%binxjc cars.xsd
– UNIX: %JAXB_HOME%/bin/xjc.sh cars.xsd
– these write generated files to current directory
• From Ant
<java jar="${env.JAXB_HOME}/lib/jaxb-xjc.jar" fork="yes">
<arg line="-d ${gen.src.dir} cars.xsd"/>
</java>

Generated Files:

• com/withoutbook/cars directory
– Car.java
• interface representing the “car” complex type
• only describes get and set methods for car properties
– Cars.java
• interface representing “cars” global element
• extends CarsType and javax.xml.bind.Element (just a marker interface)
• describes no additional methods
– CarsType.java
• interface representing anonymous complex type
defined inside the “cars” global element
• provides method to get collection of Car objects (as a java.util.List)
– ObjectFactory.java
• class used to create objects of the above interface types
• extends DefaultJAXBContextImpl which extends JAXBContext
– bgm.ser
• a serialized object of type com.sun.msv.grammar.trex.TREXGrammar
• can’t find any documentation on this - don’t know its purpose
– jaxb.properties
• sets a property that defines the class used to create JAXBContext objects

• com/withoutbook/cars/impl directory
– CarImpl.java
• class that implements Car
• corresponds to the “car” XML Schema complexType
– CarsTypeImpl.java
• class that implements CarType
• corresponds to the XML Schema anonymous type inside the “cars” element
– CarsImpl.java
• class that extends CarsTypeImpl and implements Cars
• corresponds to the “cars” XML Schema element

Is it helpful? Add Comment View Comments
 

Ques 10. How to unmarshall XML into Java objects? Convert from XML to Java objects.

• Example
ObjectFactory factory = new ObjectFactory();
Unmarshaller u = factory.createUnmarshaller();
Cars cars = (Cars) u.unmarshal(new FileInputStream("cars.xml"));
• unmarshal method accepts
– java.io.File
– java.io.InputStream
– java.net.URL
– javax.xml.transform.Source
• related to XSLT
– org.w3c.dom.Node
• related to DOM
– org.xml.sax.InputSource
• related to SAX

• Other Unmarshaller methods
– void setValidating(boolean validating)
• true to enable validation during unmarshalling; false to disable (the default)
– boolean setEventHandler(ValidationEventHandler handler)
• handleEvent method of ValidationEventHandler is called
if validation errors are encountered during unmarshalling
• default handler terminates marshalling after first error
• return true to continue unmarshalling
• return false to terminate with UnmarshalException
• see discussion of ValidationEventCollector later

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

©2024 WithoutBook