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

x
?
Solved

Saxon as validating XML Parser

Posted on 2005-03-20
3
Medium Priority
?
378 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
1 Comment
 
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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
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 tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

580 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