[JAXB] Problem with generating XML from Schema
mariuszs Feb 20, 2008 4:38 AMI have sample schema:
<?xml version="1.0" encoding="UTF-8"?> <xs:schema targetNamespace="http://www.test.com/app" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.test.com/app" elementFormDefault="qualified" version="1.0"> <xs:element name="DocSample"> <xs:complexType> <xs:sequence> <xs:element name="Element"> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
Generated objects:
// // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.1.5-b01-fcs // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> // Any modifications to this file will be lost upon recompilation of the source schema. // Generated on: 2008.02.20 at 10:25:59 AM CET // package pl.pentacomp.pentascape.da; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; /** * <p>Java class for anonymous complex type. * * <p>The following schema fragment specifies the expected content contained within this class. * * <pre> * <complexType> * <complexContent> * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * <sequence> * <element name="Element" type="{http://www.w3.org/2001/XMLSchema}anyType"/> * </sequence> * </restriction> * </complexContent> * </complexType> * </pre> * * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "element" }) @XmlRootElement(name = "DocSample", namespace = "http://www.test.com/app") public class DocSample { @XmlElement(name = "Element", namespace = "http://www.test.com/app", required = true) protected Object element; /** * Gets the value of the element property. * * @return * possible object is * {@link Object } * */ public Object getElement() { return element; } /** * Sets the value of the element property. * * @param value * allowed object is * {@link Object } * */ public void setElement(Object value) { this.element = value; } }
Java code:
@Test public void testSample() { DocSample doc = new DocSample(); doc.setElement(new String("sdsd")); StreamResult result = new StreamResult(new StringWriter()); JAXBContext jaxbContext; try { jaxbContext = JAXBContext.newInstance(DocSample.class); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.marshal(doc, result); } catch (JAXBException e) { e.printStackTrace(); } String response = result.getWriter().toString(); System.out.println(response); }
And result XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <DocSample xmlns="http://www.test.com/app"> <Element xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> sdsd </Element> </DocSample>
Is this XML correct? Why xmlns:xs, xmlns:xsi are assigned to Element, not DocSample. Please help.