Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "EclipseLink/Examples/MOXy/Dynamic/JAXBContextFromXMLSchema"
< EclipseLink | Examples | MOXy | Dynamic
(→Bootstrap the JAXBContext) |
(→Bootstrap the JAXBContext) |
||
Line 14: | Line 14: | ||
public static void main(String[] args) throws Exception { | public static void main(String[] args) throws Exception { | ||
− | FileInputStream xsdInputStream = new FileInputStream("src/example | + | FileInputStream xsdInputStream = new FileInputStream("src/example/customer.xsd"); |
DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromXSD(xsdInputStream, null, null, null); | DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromXSD(xsdInputStream, null, null, null); | ||
} | } |
Revision as of 11:22, 24 June 2010
Overview
In this example you will learn how to bootstrap a dynamic JAXBContext from an XML Schema.
Bootstrap the JAXBContext
The DynamicJAXBContextFactory is used to create a dynamic JAXBContext. The createContextFromXSD method can be used to bootstrap a DynamicJAXBContext from an XML Schema. The first parameter represents the XML schema itself and must be in one of the following forms: java.io.InputStream, org.w3c.dom.Node, or javax.xml.transform.Source.
import java.io.FileInputStream; import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext; import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory; public class Demo { public static void main(String[] args) throws Exception { FileInputStream xsdInputStream = new FileInputStream("src/example/customer.xsd"); DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromXSD(xsdInputStream, null, null, null); } }
XML Schema
The following XML schema represents the metadata for this JAXBContext.
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="www.example.org" targetNamespace="www.example.org" elementFormDefault="qualified"> <xsd:complexType name="address"> <xsd:sequence> <xsd:element name="street" type="xsd:string" minOccurs="0"/> <xsd:element name="city" type="xsd:string" minOccurs="0"/> </xsd:sequence> </xsd:complexType> <xsd:element name="customer"> <xsd:complexType> <xsd:sequence> <xsd:element name="name" type="xsd:string" minOccurs="0"/> <xsd:element name="address" type="address" minOccurs="0"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>