S
sk
I am trying to make validation program but getting the following error.
org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of
element 'tag1'.
I attached the xml document and schema as well as the java program to
validate the xml doc.
//java program to validate the xml document
DocumentBuilder parser =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = parser.parse(new File("c:\\myxmldoc.xml"));
// Create a SchemaFactory capable of understanding WXS schemas.
SchemaFactory factory =
SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
// Load a WXS schema, represented by a Schema instance.
javax.xml.transform.Source schemaFile = new
javax.xml.transform.stream.StreamSource(new File("c:\\XMLType2.xsd"));
Schema schema = factory.newSchema(schemaFile);
// Create a Validator object, which can be used to validate
Validator validator = schema.newValidator();
// Validate the DOM tree.
validator.validate(new javax.xml.transform.dom.DOMSource(document));
//xml document////////
<?xml version="1.0"?>
<tag1 >
hello world
</tag1>
//schema///////////////////
<?xml version="1.0" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="tag1">
<xs:simpleType>
<xs:restriction base="xs:string" >
<xs:maxLength value="50" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>
org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of
element 'tag1'.
I attached the xml document and schema as well as the java program to
validate the xml doc.
//java program to validate the xml document
DocumentBuilder parser =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = parser.parse(new File("c:\\myxmldoc.xml"));
// Create a SchemaFactory capable of understanding WXS schemas.
SchemaFactory factory =
SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
// Load a WXS schema, represented by a Schema instance.
javax.xml.transform.Source schemaFile = new
javax.xml.transform.stream.StreamSource(new File("c:\\XMLType2.xsd"));
Schema schema = factory.newSchema(schemaFile);
// Create a Validator object, which can be used to validate
Validator validator = schema.newValidator();
// Validate the DOM tree.
validator.validate(new javax.xml.transform.dom.DOMSource(document));
//xml document////////
<?xml version="1.0"?>
<tag1 >
hello world
</tag1>
//schema///////////////////
<?xml version="1.0" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="tag1">
<xs:simpleType>
<xs:restriction base="xs:string" >
<xs:maxLength value="50" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>