JAXB%20Interview%20Questions%20and%20Answers
Question: Java code for marshalling Java objects into XML.Answer: • ExampleMarshaller 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 |
Is it helpful?
Yes
No
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?