JAXB Interviewfragen und Antworten
Frage 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
Frage 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>
Frage 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>
Frage 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
Frage 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
Am hilfreichsten laut Nutzern:
- What Is XML Binding?
- What is XML Binding Relationships?
- Please explain JAXB Use Cases.
- Why Use XML Binding?
- What are the goals of JAXB?