Searchuing for a job in JXAB? A blend of XML and Java technology are looked as building blocks for developing applications of web service. This is the juncture where JAXB has become popular. It provides a convenient way to for developers to implement XML data in Java applications. Job seekers of JAXB are pretty in demand by the corporate companies. Let you not miss the deal of getting placed in the multinational companies. A resourceful number of JXAB interview questions and answers at wisdiomjobs.com will help you in this regard. Wisdomjobs portal stands by your side in not only helping you with the right placement details but also with the job interview questions and answers to get placed in your desired role.
Question 1. What Is Xml Binding?
Answer :
Maps XML to in-memory objects according to a schema
Generates classes to represent XML elements:
Supports three primary operations:
includes validation of the XML against the schema:
used to generate the classes of the objects validation of object trees against the schema used to generate their classes
Question 2. What Is Xml Binding Relationships?
Answer :
The relationships between the components involved in XML binding (data binding) are shown below schema -> classes XML -> schema Schema generates classes.
Question 3. Why Use Xml Binding?
Answer :
It’s not necessary
It’s easier
It’ s less error-prone
It can allow customization of the XML structure
Question 4. Explain Jaxb Use Cases?
Answer :
Create/Read/Modify XML using Java
Validate user input:
Use XML-based configuration files
Question 5. What Are The Goals Of Jaxb?
Answer :
Easy to use:
Customizable:
Portable:
Deliver soon:
Natural:
Match schema:
Hide plumbing:
Validation on demand:
Preserve object equivalence:
(round tripping)
Question 6. What Are The Disadvantages/non-goals Of Jaxb?
Answer :
Standardize generated Java:
Preserve XML equivalence:
Bind existing JavaBeans to schemas:
Schema evolution support:
Allow generated Java to access:
XML elements/attributes not described in initial schema
Partial binding:
Implement every feature of the schema language:
Support DTDs:
Question 7. Give Me An Example Of Xsd?
Answer :
<?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>
Question 8. Give Me An Example Of Xml Document?
Answer :
<?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>
Question 9. How To Generate Java From Xml Schema. Please Show The Example?
Answer :
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:
Cars.java:
CarsType.java:
ObjectFactory.java:
bgm.ser:
jaxb.properties:
CarImpl.java:
CarsTypeImpl.java:
CarsImpl.java:
Question 10. How To Unmarshall Xml Into Java Objects? Convert From Xml To Java Objects?
Answer :
Example:
ObjectFactory factory = new ObjectFactory();
Unmarshaller u = factory.createUnmarshaller();
Cars cars = (Cars) u.unmarshal(new FileInputStream(“cars.xml”));
unmarshal method accepts:
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 UnmarshalExceptio
• see discussion of ValidationEventCollector later
Question 11. Java Example/java Program To Set Object For Generating Xml?
Answer :
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);
Question 12. How To Validate Java Objects?
Answer :
The graph of Java objects can contain invalid data:
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
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();
Question 13. Explain Customizing Type Bindings?
Answer :
Default bindings can be overridden:
Customizations include
Question 14. Explain The Syntax Of Customization?
Answer :
Customizations can be specified in
The XML Schema must declare
the JAXB namespace and version
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>
Question 15. Discuss Customization Levels?
Answer :
Customizations can be made at four levels
global:
schema:
definition:
component:
Question 16. Discuss Global Bindings Attributes?
Answer :
collectionType:
enableFailFastCheck:
generateIsSetMethod:
underscoreBinding
bindingStyle (was modelGroupAsClass):
choiceContentProperty:
enableJavaNamingConventions:
fixedAttributeAsConstantProperty:
typesafeEnumBase:
typesafeEnumMemberName:
Question 17. What Is The Syntax Of Schemabindings?
Answer :
The syntax for the schemaBindings element is
<jxb:schemaBindings>
<jxb:package [name="package-name"]>
<jxb:javadoc> ... javadoc ... </jxb:javadoc>
</package>
<jxb:nameXmlTransform>
<jxb:typeName prefix="prefix" suffix="suffix"/>
<jxb:elementName prefix="prefix" suffix="suffix"/>
<jxb:modelGroupName prefix="prefix" suffix="suffix"/>
<jxb:anonymousTypeName prefix="prefix" suffix="suffix"/>
</jxb:nameXmlTransform>
</jxb:schemaBindings>
– every element and attribute within schemaBindings is optional
Question 18. Java Code For Marshalling Java Objects Into Xml?
Answer :
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:
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 Related Tutorials |
|
---|---|
Java Script Tutorial | J2EE Tutorial |
Core Java Tutorial | AJAX Tutorial |
Java-Springs Tutorial | Hibernate Tutorial |
Spring MVC Framework Tutorial | XML-RPC Tutorial |
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.