Validator.validate() fails when using DOM Document instead of File
Dear Java 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 a 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.
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"?>
<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"/>
<workflow id="4654" name="Article"/>
<outputChannel id="4518" name="Print"/>
<variant id="-1" name=""/>
Validation source code snippet:
dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document newDoc = db.newDocument();
//inXMLNode is the XML structured above
//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);
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.
Couldn't create an account
Oracle forums wouldn't let me create an account when I tried. I guess it would probably be better asked there, I guess I'm going to have to try it again.