简体   繁体   中英

Validating xml with xsd using JDOM

HI I am trying to validate an xml with an xsd using JDOM . Here is my code for validation :-

public class jdom1 {

    public static void main(String[] args) throws JDOMException, IOException {

        // TODO Auto-generated method stub
        File xsdfile = new File("file.xsd");
        XMLReaderJDOMFactory schemafac = new XMLReaderXSDFactory(xsdfile);
        SAXBuilder builder = new SAXBuilder(schemafac);
        Document doc =  builder.build(new File("file.xml"));
        Element root = doc.getRootElement();
        for(Element testCase : root.getChildren()){
            //Code

    }

}

My file.xml :-

<?xml version="1.0" encoding="UTF-8"?>


<document>
 <testCaseDataName>
  <id>1</id >
  <testCaseName>Edit</testCaseName >
  <expectedResult>true</expectedResult >

 </testCaseDataName>
 <testCaseDataName>
  <id>2</id >
  <testCaseName>add</testCaseName >
  <expectedResult>false</expectedResult>
  <parameter>
    <key>featues</key >
    <value>w,f</value>
    </parameter>
 </testCaseDataName>

  <testCaseDataName>
  <id>3</id >
  <testCaseName>delete</testCaseName >
  <expectedResult>duplicate</expectedResult>
  <parameter>
    <key>projectType</key >
    <value>new</value>
    <key>Name</key >
    <value>mashnew</value>
    <key>status</key >
    <value>ACTIVE</value>
    <key>canOrder</key >
    <value>Yes</value>
    </parameter>
 </testCaseDataName>
 </document>

My file.xsd :-

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.example.org/ACL"
    xmlns:tns="http://www.example.org/ACL"
    elementFormDefault="qualified">
    <include schemaLocation=""></include>
    <element name="document">
    <complexType>
      <sequence>
    <element name="testCaseDataName">
     <complexType>
      <sequence>
        <element name ="id" type ="ID" ></element>
        <element name ="testCaseName" type ="string" ></element>
        <element name = "expectedResult" type="string"></element>
        <element name = "parameter" minOccurs="0">
        <complexType>
            <sequence>
                <element name="key" type ="string" maxOccurs="1"></element>
                <element name="value" type="string" maxOccurs="unbounded"></element>
            </sequence>
        </complexType>
        </element>
        </sequence>
     </complexType>
    </element>
    </sequence>
    </complexType>
    </element>
</schema>

When I run this code I get the following error. Can anyone please help me fix this .

Exception in thread "main" org.jdom2.input.JDOMParseException: Error on line 4 of document file:/C:/Users/file.xml: cvc-elt.1: Cannot find the declaration of element 'document'.
    at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:228)
    at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:277)
    at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:264)
    at org.jdom2.input.SAXBuilder.build(SAXBuilder.java:1116)
    at com.memoir.client.test.testdb.jdom1.main(jdom1.java:24)
Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'document'.
    at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
    at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
    at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
    at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
    at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
    at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:217)
    ... 4 more

This is the way to do validation can you please check your code

SchemaFactory schemafac =
     SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
     Schema schema = schemafac.newSchema(new File("myschema.xsd"));
     XMLReaderJDOMFactory factory = new XMLReaderSchemaFactory(schema);
     SAXBuilder sb = new SAXBuilder(factory);
     Document doc = sb.build(new File("file.xml"));

use below schema

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="document">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="testCaseDataName" maxOccurs="unbounded" minOccurs="0">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:byte" name="id"/>
              <xs:element type="xs:string" name="testCaseName"/>
              <xs:element type="xs:string" name="expectedResult"/>
              <xs:element name="parameter" minOccurs="0">
                <xs:complexType>
                  <xs:choice maxOccurs="unbounded" minOccurs="0">
                    <xs:element type="xs:string" name="key"/>
                    <xs:element type="xs:string" name="value"/>
                  </xs:choice>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

My schema was wrong. Used this http://www.freeformatter.com/xsd-generator.html to generate a schema and the validation is working fine. Thanks

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM