Results 1 to 5 of 5
  1. #1
    msucena is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Unhappy 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.

    Exception:
    cvc-elt.1: Cannot find the declaration of element 'k4Object': where 'k4Object' is my root element.


    DOM Document with a Namespace:
    Java Code:
    <?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:
    Java Code:
            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

  2. #2
    msucena is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default Please help

    Does anyone have anything to say about this?

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    Didn't you ask this on the Oracle forum?

  4. #4
    msucena is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default 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.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    Oh, hang on.
    I misread the error.
    I'm mangling my forums together!
    :)

Similar Threads

  1. Validate a Zip file with java
    By peuplier in forum Advanced Java
    Replies: 1
    Last Post: 07-10-2009, 10:07 PM
  2. Validate document starting at xpath
    By sarcasteak in forum Advanced Java
    Replies: 1
    Last Post: 04-16-2009, 01:22 AM
  3. Replies: 7
    Last Post: 05-23-2008, 03:46 AM
  4. Replies: 3
    Last Post: 08-01-2007, 05:15 PM
  5. Replies: 1
    Last Post: 07-06-2007, 04:39 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •