Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does this SAXParseException get thrown?
    text
    copied!<p>I am somewhat at a loss with this problem. I am writing a short section of code that should validate an XML file against the corresponding XSD Schema. To test, I passed it a valid XML file (validated by whatever Netbeans uses). To my chagrin I received the following error message:</p> <p><em>org.xml.sax.SAXParseException; cvc-elt.1: Cannot find the declaration of element 'map'.</em></p> <p>This is strange given that Netbeans found the declaration of element 'map' just fine. I know that relative file paths can sometimes be a problem, so I substituted an absolute path. Result: same problem.</p> <p>As a result I can only assume that the <code>SAXParser</code> doesn't use the <code>xsi:schemaLocation</code> attribute to determine the Schema file.</p> <p>Does anyone know why I am expriencing this behaviour?</p> <p>Here is an SSCCE:</p> <pre><code>import java.io.File; import java.io.FileInputStream; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Source; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import javax.xml.validation.Validator; import org.w3c.dom.Document; import org.xml.sax.SAXException; public class ValidatorTest { public static void main(String[] args) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); Document doc = null; try { DocumentBuilder db = dbf.newDocumentBuilder(); doc = db.parse(new FileInputStream(new File(args[0]))); } catch (Throwable ex) { ex.printStackTrace(); System.exit(1); } SchemaFactory sf = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); Schema schema = null; try { schema = sf.newSchema(new StreamSource(new File(args[1]))); } catch (SAXException ex) { ex.printStackTrace(); System.exit(1); } Source source = new DOMSource(doc); Validator validator = schema.newValidator(); try { validator.validate(source); System.out.println("SUCESS!"); } catch (SAXException ex) { ex.printStackTrace(); System.out.println("FAIL!"); } catch (IOException ex) { ex.printStackTrace(); System.exit(1); } } } </code></pre> <p>My XML:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;map id="testMap" name="Test Map" xmlns="zorkCloneMapTypes" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="zorkCloneMapTypes /home/max/NetBeanzWorkspace/ZorkClone/src/main/resources/maps/betterMaps.xsd"&gt; &lt;room id="testingGrounds" name="Testing Grounds" posX="0" posY="1"&gt; &lt;type&gt;basic&lt;/type&gt; &lt;description&gt;A dusty arena, enclosed in a circular, wooden fence. Clearly designed for practicing combat. There are red blood spatters in the sand.&lt;/description&gt; &lt;passageNorth&gt;true&lt;/passageNorth&gt; &lt;passageEast&gt;false&lt;/passageEast&gt; &lt;passageSouth&gt;false&lt;/passageSouth&gt; &lt;passageWest&gt;false&lt;/passageWest&gt; &lt;enemies&gt; &lt;enemy&gt; &lt;name&gt;Training Master&lt;/name&gt; &lt;type&gt;Basic&lt;/type&gt; &lt;description&gt;A Training Master. He will not let you pass until you defeat him in combat.&lt;/description&gt; &lt;level&gt;1&lt;/level&gt; &lt;/enemy&gt; &lt;/enemies&gt; &lt;containers&gt; &lt;container locked="false"&gt; &lt;name&gt;Equipment locker.&lt;/name&gt; &lt;description&gt;Equipment locker.&lt;/description&gt; &lt;level&gt;5&lt;/level&gt; &lt;/container&gt; &lt;container locked="false"&gt; &lt;name&gt;Equipment locker.&lt;/name&gt; &lt;description&gt;Equipment locker.&lt;/description&gt; &lt;level&gt;5&lt;/level&gt; &lt;/container&gt; &lt;/containers&gt; &lt;/room&gt; &lt;transferRoom id="start" name="Starting Map" posX="0" posY="0"&gt; &lt;type&gt;transfer&lt;/type&gt; &lt;description&gt;The starting room for the game.&lt;/description&gt; &lt;transferID&gt;testMap.testingGrounds&lt;/transferID&gt; &lt;passageNorth&gt;false&lt;/passageNorth&gt; &lt;passageEast&gt;false&lt;/passageEast&gt; &lt;passageSouth&gt;true&lt;/passageSouth&gt; &lt;passageWest&gt;false&lt;/passageWest&gt; &lt;enemies/&gt; &lt;containers/&gt; &lt;/transferRoom&gt; &lt;/map&gt; </code></pre> <p>And my XSD:</p> <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="zorkCloneMapTypes" targetNamespace="zorkCloneMapTypes" elementFormDefault="qualified"&gt; &lt;xs:element name="map"&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;xs:element minOccurs="0" maxOccurs="unbounded" ref="room"/&gt; &lt;xs:element minOccurs="1" maxOccurs="unbounded" ref="transferRoom"/&gt; &lt;/xs:sequence&gt; &lt;xs:attribute name="id" type="xs:ID" use="required"/&gt; &lt;xs:attribute name="name" type="xs:string" use="required"/&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;xs:element name="transferRoom"&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;xs:element ref="type"/&gt; &lt;xs:element ref="description"/&gt; &lt;xs:element ref="transferID"/&gt; &lt;xs:element ref="passageNorth"/&gt; &lt;xs:element ref="passageEast"/&gt; &lt;xs:element ref="passageSouth"/&gt; &lt;xs:element ref="passageWest"/&gt; &lt;xs:element minOccurs="0" maxOccurs="unbounded" ref="enemies"/&gt; &lt;xs:element minOccurs="0" maxOccurs="unbounded" ref="containers"/&gt; &lt;/xs:sequence&gt; &lt;xs:attribute name="name" use="required" type="xs:string"/&gt; &lt;xs:attribute name="id" use="required" type="xs:ID"/&gt; &lt;xs:attribute name="posX" use="required" type="xs:string"/&gt; &lt;xs:attribute name="posY" use="required" type="xs:string"/&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;xs:element name="room"&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;xs:element ref="type"/&gt; &lt;xs:element ref="description"/&gt; &lt;xs:element ref="passageNorth"/&gt; &lt;xs:element ref="passageEast"/&gt; &lt;xs:element ref="passageSouth"/&gt; &lt;xs:element ref="passageWest"/&gt; &lt;xs:element minOccurs="0" maxOccurs="unbounded" ref="enemies"/&gt; &lt;xs:element minOccurs="0" maxOccurs="unbounded" ref="containers"/&gt; &lt;/xs:sequence&gt; &lt;xs:attribute name="name" use="required" type="xs:string"/&gt; &lt;xs:attribute name="id" use="required" type="xs:ID"/&gt; &lt;xs:attribute name="posX" use="required" type="xs:string"/&gt; &lt;xs:attribute name="posY" use="required" type="xs:string"/&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;xs:element name="containers"&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;xs:element minOccurs="0" maxOccurs="unbounded" ref="container"/&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;xs:element name="enemies"&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;xs:element minOccurs="0" maxOccurs="unbounded" ref="enemy"/&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;xs:element name="container"&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;xs:element ref="name"/&gt; &lt;xs:element ref="description"/&gt; &lt;xs:element ref="level"/&gt; &lt;/xs:sequence&gt; &lt;xs:attribute name="locked" default="false" type="xs:boolean"/&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;xs:element name="enemy"&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;xs:element ref="name"/&gt; &lt;xs:element ref="type"/&gt; &lt;xs:element ref="description"/&gt; &lt;xs:element ref="level"/&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;xs:element name="name" type="xs:string"/&gt; &lt;xs:element name="type" type="xs:string"/&gt; &lt;xs:element name="description" type="xs:string"/&gt; &lt;xs:element name="transferID" type="xs:string"/&gt; &lt;xs:element name="passageNorth" type="xs:boolean"/&gt; &lt;xs:element name="passageEast" type="xs:boolean"/&gt; &lt;xs:element name="passageSouth" type="xs:boolean"/&gt; &lt;xs:element name="passageWest" type="xs:boolean"/&gt; &lt;xs:element name="level" type="xs:int"/&gt; &lt;/xs:schema&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload