Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1799
  • Last Modified:

Trying to validate a xml schema with JDOM

I'm trying to validate an xml file with a schema i created using JDOM and xerces 2.  Here is my java code:

    SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser");
    builder.setValidation(true);
    builder.setErrorHandler(new BestSAXChecker());
    // turn on schema support
    builder.setFeature("http://apache.org/xml/features/validation/schema", true);  
            builder.setProperty(
                    "http://apache.org/xml/properties/schema/external-schemaLocation",
                    "http://localhost:8081/roadloans_application.xsd roadloans_application.xsd" );
                             
      File file =  loadStream( "c:/Projects/schema_validating/testdata.xml" );
      if(file == null)
            System.err.println("Error with stream");
      
      
    try
      {
      Document doc = builder.build(file);
    }
    // indicates a well-formedness error
    catch (JDOMException e) {
      System.out.println(args[0] + " is not well-formed.");
      System.out.println(e.getMessage());
    }  
    catch (IOException e) {
      System.out.println("Could not check " + args[0]);
      System.out.println(" because " + e.getMessage());
    }  


The code is not mine just got it off the web.   BestSAXChecker class is just a class that does system outs.

The schema is:
<?xml version="1.0" ?>
<xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" >

      <xsd:element name="LoanApplication">
            <xsd:complexType>
                  <xsd:sequence>
                        <xsd:element ref="ApplicationList" />
                  </xsd:sequence>
            </xsd:complexType>
      </xsd:element>
      
      <xsd:element name="ApplicationList">
            <xsd:complexType>
                  <xsd:sequence>
                        <xsd:element ref="ApplicantData" />
                  </xsd:sequence>
                  <xsd:attribute id="partner_code" type="xsd:unsignedLong" />
            </xsd:complexType>
      </xsd:element>
      
      <xsd:element name="ApplicantData">
            <xsd:attribute name="for">
                  <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                              <xsd:enumeration value="PRIMARY_APPLICANT"/>
                              <xsd:enumeration value="COBORROWER_APPLICANT"/>
                        </xsd:restriction>
                  </xsd:simpleType>
            </xsd:attribute>
      </xsd:element>

</xsd:schema>

The xml files is:
<?xml version="1.0" ?>
<LoanApplication>
      <ApplicationList partner_code="121233">
            <ApplicantData for="COBORROWER_APPLICANT">
            </ApplicantData>
      </ApplicationList>
</LoanApplication>


When i run the code i keep getting the same error:
Error: cvc-elt.1: Cannot find the declaration of element 'LoanApplication'.
 at line 2, column 18
 in entity file:/c:/Projects/schema_validating/testdata.xml

I have LoanApplication in my xml so i'm not quite sure what is going on.  
Any ideas on what is going on here?

Thanks
0
borg48
Asked:
borg48
  • 2
  • 2
1 Solution
 
ramiseesniblicCommented:
Just to try something:


Try changing the line:

          builder.setProperty(
                 "http://apache.org/xml/properties/schema/external-schemaLocation",
                 "http://localhost:8081/roadloans_application.xsd roadloans_application.xsd" );

to:
         builder.setProperty(
                    "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
                    "http://localhost:8081/roadloans_application.xsd roadloans_application.xsd" );

0
 
ramiseesniblicCommented:
Just a bit more.

This is how I have done Xml Validation, using xerces2

      try {
        DOMParser aParser = new DOMParser();
        aParser.setErrorHandler(new XmlParseHandler());
        aParser.setFeature("http://xml.org/sax/features/validation", true);
        aParser.setFeature("http://apache.org/xml/features/validation/schema", true);
        aParser.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
        aParser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", <theXsdLocation>);

        aParser.parse(theXmlLocation);

      }catch(Throwable e1) {
        StackTraceElement[] stackTrace = e1.getStackTrace();
        log.info("ERROR MESSAGE: " + e1.getMessage());
        e1.printStackTrace();
        throw new Exception("Error Parsing File " + theXmlLocation, e1);
      }
0
 
borg48Author Commented:
I tried the change you mentioned and still got the error.  I'm trying your DOMParser but i'm not sure what package XmlParseHanlder is.  Does it require another library aside from JDOM and Xerces?


0
 
borg48Author Commented:
If i use your code and the original one and modify the following setProperty:
 "http://localhost:8081/roadloans_application.xsd roadloans_application.xsd" );
to
 "http://localhost:8081/roadloans_application.xsd" );

I get the error:
The prefix "xsd" for element "xsd:schema" is not bound.

with my original code the same error but with alitle more detail:
The prefix "xsd" for element "xsd:schema" is not bound.  At line 2, column 55 in entity http://localhost:8081/roadloans_application.xsd

Is this correct:
 "http://localhost:8081/roadloans_application.xsd roadloans_application.xsd" );
or should it be:
 "http://localhost:8081/roadloans_application.xsd" );
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now