I've following schema using which i have generated the jaxb object. I'm populating the jaxb object with data and then marshaling it.I want to do schema validation while marshaling the jaxb object.
ByteArrayOutputStream formXml = new ByteArrayOutputStream();
new JAXBElement<Form100DIV_V100>(new QName("http://example.org/types/2003/04", "Form100DIV_V100"), Form100DIVV100.class, (Form100DIVV100) form100);
if (isSchemaValidationNeeded) {
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
InputStream xsdStream = XmlUtil.class.getClassLoader().getResourceAsStream("schema/form.xsd");
StreamSource xsdSource = new StreamSource(xsdStream);
Schema schema = sf.newSchema(xsdSource);
//m.setEventHandler(new SchemaValidationEventHandler());
//m.setSchema(schema);
Validator validator = schema.newValidator();
try {
validator.validate(new StreamSource(new ByteArrayInputStream(formXml.toByteArray())));
System.out.println("File is valid");
} catch (SAXException e) {
System.out.println("File is NOT valid");
System.out.println("Reason: " + e.getLocalizedMessage());
} catch (IOException e) {
e.printStackTrace();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://example.org/types/2003/04" targetNamespace="http://example.org/types/2003/04"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:complexType name="Form100DIV_V100">
<xs:complexContent>
<xs:extension base="AbstractForm100">
<xs:sequence>
<xs:element name="AMOUNT" type="AmountType" minOccurs="0"/>
---
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
----
-----
</xs:schema>
Here is the xml which i'm getting after marshaling
<Form100DIV_V100 xmlns="http://example.org/types/2003/04">
<AMOUNT>100.00</AMOUNT>
-------
---------
</Tax1099Div_V100>
I'm getting below error though namespaces are correct in xml and xsd. Reason: cvc-elt.1: Cannot find the declaration of element 'Form100DIV_V100'.
Form100DIV_V100
is not defined in your schema as a top level element, only as a type. You can simply just wrap the xs:complexType
in a xs:element
<xs:element name="Form100DIV_V100"> <xs:complexType> <xs:complexContent> <xs:extension base="AbstractForm100"> <xs:sequence> <xs:element name="AMOUNT" type="AmountType" minOccurs="0" /> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType> </xs:element>
In your xml instance, <Form100DIV_V100>
is not terminated correctly. You are terminating it with </Tax1099Div_V100>
.
<Form100DIV_V100 xmlns="http://example.org/types/2003/04"> <AMOUNT>100.00</AMOUNT> </Form100DIV_V100>
Given you have AbstractForm100
and AmountType
types correctly defined and the rest of your -----
s are correct, the above fixes should validate.
Also, compiling with xjc should give you a Form100DIVV100
defined with a necessary @XmlRootElement
annotation for your class
@XmlRootElement(name = "Form100DIV_V100")
public class Form100DIVV100 extends AbstractForm100 {
Though it doesn't look like
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.