iman453
iman453

Reputation: 9535

XML validation error, Element not found (even though element exists and schema and xml are loaded properly, I think)

I'm trying to validate a simple XML file against an XML schema using the following chunk of code. I'm getting an exception

org.xml.sax.SAXParseException: cvc-elt.1: 
    Cannot find the declaration of element 'Applications'

and was wondering if anyone had any idea why that could be. (When I try looking at what the contents of the schema are after it is loaded, I see a bunch of grammar objects or something of that sort, with nothing indicating that the schema is loaded properly. Could that be it? If I try loading a schema with an non-existing file name, it gives me a file not found exception...so I'm getting it is finding the right schema when the right file name is given)

public void getPriceSummaryInfo(){
        Document doc = null;
        try {
            File fXmlFile = new File("testXML.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            doc = dBuilder.parse(fXmlFile);
        } 
        catch (Exception e) {
            e.printStackTrace();
        }

        try {
        Schema schema = getSchema("testSchema.xsd");
        Validator validator = schema.newValidator();
        validator.validate(new DOMSource(doc));
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Schema getSchema (String xsdPath) throws Exception {
        assert xsdPath != null : "XML schema path is null.";
        SchemaFactory fact = 
                SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

        Schema schema;
        try {
            schema = fact.newSchema(new StreamSource(new File(xsdPath)));
        }
        catch (SAXException e) {
            throw new Exception(
                    "Unable to find target schema to validate XML.", e);
        }
        return schema;
    } 

The xml and xsd files are:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.randomthing.com"
            xmlns="http://www.randomthing.com"
            elementFormDefault="qualified">

    <xsd:element name="Applications">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="Application" minOccurs="0" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="Application">
        <xsd:annotation>
            <xsd:documentation>
             blah
            </xsd:documentation>
        </xsd:annotation>
       <xsd:complexType>
            <xsd:attribute name="id" type="xsd:long" use="required"/>
            <xsd:attribute name="name" type="xsd:string" use="required"/>
            <xsd:attribute name="order" type="xsd:long" use="required"/>
        </xsd:complexType>    
    </xsd:element>
</xsd:schema>  



<?xml version="1.0" encoding="UTF-8"?>
<Applications xmlns ="http://www.randomthing.com"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.randomthing.com testSchema.xsd">

    <Application id="3" name="Blah" order="2">
    </Application>
</Applications>

Would appreciate any pointers! Thanks

Upvotes: 0

Views: 2125

Answers (1)

jtahlborn
jtahlborn

Reputation: 53694

You should parse the document with namespaces, you are losing them at load time.

dbFactory.setNamespaceAware(true);

Upvotes: 1

Related Questions