?
Solved

Saxon as validating XML Parser

Posted on 2005-03-20
3
Medium Priority
?
364 Views
Last Modified: 2013-11-23
Hello,

I'm having a problem using Saxon as a validating XML Parser.

The code I have looks like this:
try {
   System.setProperty("javax.xml.parsers.DocumentBuilderFactory","org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
   Factory.setNamespaceAware(true);
   factory.setValidating(true);
   factory.setNamespaceAware(false);
   factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage","http://www.w3.org/2001/XMLSchema");
   factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource",schema);
   DocumentBuilder builder = factory.newDocumentBuilder();
   Validator handler = new Validator();
   builder.setErrorHandler(handler);
   builder.parse(rawXML);
   if (handler.validationError == true) {
      System.out.println("XML Document has Error:" + handler.validationError + " " + handler.saxParseException.getMessage());
   }            
} catch (java.io.IOException ioe) {
   System.out.println("IOException " + ioe.getMessage());
}

The XML schema represented in the code above as the variable schema looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
      <xs:element name="ROW">
            <xs:complexType>
                  <xs:sequence>
                        <xs:element name="ZIP">
                              <xs:simpleType>
                                    <xs:restriction base="xs:int"/>
                              </xs:simpleType>
                        </xs:element>
                        <xs:element name="CITY">
                              <xs:simpleType>
                                    <xs:restriction base="xs:string"/>
                              </xs:simpleType>
                        </xs:element>
                        <xs:element name="COUNTRY">
                              <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                          <xs:pattern value="[A-Z]{2}"/>
                                    </xs:restriction>
                              </xs:simpleType>
                        </xs:element>
                        <xs:element name="CITYCODE">
                              <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                          <xs:pattern value="([A-Z]|[0-9]){3}"/>
                                    </xs:restriction>
                              </xs:simpleType>
                        </xs:element>
                        <xs:element name="ADDRESS">
                              <xs:simpleType>
                                    <xs:restriction base="xs:string"/>
                              </xs:simpleType>
                        </xs:element>
                        <xs:element name="BECODE">
                              <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                          <xs:pattern value="[A-Z]{1}[0-9]{3}"/>
                                    </xs:restriction>
                              </xs:simpleType>
                        </xs:element>
                        <xs:element name="NAME">
                              <xs:simpleType>
                                    <xs:restriction base="xs:string"/>
                              </xs:simpleType>
                        </xs:element>
                  </xs:sequence>
            </xs:complexType>
      </xs:element>
</xs:schema>

And finally the XML I-m trying to validate looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<ROW>
  <ZIP>90660</ZIP>
  <CITY>Pico Rivera</CITY>
  <COUNTRY>US</COUNTRY>
  <CITYCODE>PIU</CITYCODE>
  <ADDRESS>NULL</ADDRESS>
  <BECODE>ABFS</BECODE>
  <NAME>ABF FREIGHT SYSTEM INC.</NAME>
</ROW>

When I run the code a MalformedURLException is thrown from the parse method above and the detailed message says:
no protocol: <?xml version="1.0" encoding="UTF-8"?>
<ROW>
  <ZIP>NULL</ZIP>
  <CITY>NULL</CITY>
  <COUNTRY>NULL</COUNTRY>
  <CITYCODE>NULL</CITYCODE>
  <ADDRESS>NULL</ADDRESS>
  <BECODE>AATW</BECODE>
  <NAME>AAA Trucking</NAME>
</ROW>

Has anyone got a clue as to what goes wrong here?

Regards
John
0
Comment
Question by:JohnBak
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 13586625
Make sure that all input entites have a protocol where a url is expected. E.g. suppose 'schema' holds the value myschema.xml and it's a file. That should be file:myschema.xml if in the current directory
0

Featured Post

Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
This video teaches viewers about errors in exception handling.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses
Course of the Month12 days, 22 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question