Validator.validate() fails with DOM Document containing a Namespace
810632Nov 4 2010 — edited Nov 8 2010Dear Oracle Community:
I have had a heck of a time tracking down the problem I am seeing. I am trying to validate a DOM Document with a Namespace against an XSD Schema. I am attempting to use the javax.xml.validation package with Java 6. No matter what I do I keep getting the following exception. Please help.
I am certainly open to other solutions as well, e.g. JAXP, SAX, etc.
Exception:
cvc-elt.1: Cannot find the declaration of element 'k4Object': where 'k4Object' is my root element.
DOM Document with a Namespace:
<?xml version="1.0" encoding="UTF-8"?>
<k4Object xmlns="http://www.company.com/InPorterIntermediate">
<k4Server name="Tech_K4_6" url="http://eng10as:8080/K4/"/>
<k4Login password="x4" username="admin"/>
<template id="-1" name=""/>
<publication id="1280178296340" name="InPortTesting"/>
<issue dynamic="" id="1510" name="Testing 2"/>
<section id="7" name="Editorial"/>
<notes value=""/>
<workflow id="4654" name="Article"/>
<outputChannel id="4518" name="Print"/>
<variant id="-1" name=""/>
</k4Object>
Validation source code snippet:
DocumentBuilderFactory dbf;
dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document newDoc = db.newDocument();
//inXMLNode is the XML structured above
newDoc.appendChild(newDoc.importNode(inXMLNode, true));
//Lookup a factory for the W3C XML Schema language
SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
//Compile the schema.
//Here the schema is loaded from a resource
InputStream schemaLocation = this.getClass().getResourceAsStream("/InPorterIntermediate.xsd");
Source schemaSource = new StreamSource(schemaLocation);
Schema schema = factory.newSchema(schemaSource);
//Get a validator from the schema.
Validator validator = schema.newValidator();
+Source source = new DOMSource(newDoc);+
validator.validate(source);
log.info("Merged document is valid.");
The most disturbing fact is that I have this exact same code in another location that works fine. The difference is that instead of using Source source = new DOMSource(newDoc), I am using Source source = new StreamSource(file). I can't transform my DOM Document to a file and use that since doing a transformation on it would also not be Namespace aware.
Note: If I put the XML into and editor like oXygen and create a validation scenario with my physical XSD Schema file it states the XML is valid.
Please give me some assistance, any information would be of great use. Thank you.
Sincerely,
Mike Sucena