0
我使用了下面的模式來生成jaxb對象。我使用數據填充jaxb對象,然後編組它。我想在編組jaxb對象時進行模式驗證。cvc-elt.1:無法找到RootElement的元素聲明
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>
這裏是我編組
<Form100DIV_V100 xmlns="http://example.org/types/2003/04">
<AMOUNT>100.00</AMOUNT>
-------
---------
</Tax1099Div_V100>
我得到下面的錯誤,雖然名稱空間是XML和XSD無誤後讓我的XML。 原因:cvc-elt.1:找不到元素「Form100DIV_V100」的聲明。