Skip to main content

Unable to do schema validation - cannot found class

5 replies [Last post]
Joined: 2004-05-13

I am trying to perform marshalling/unmarshalling per
the example in the samples (schemagen).

Here is a snippet of the exception:

- with linked exception:
[org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'ddnRequest'.]
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(
Testsuite: ddn.util.RequestTest
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(

I annotated the java classes (DdnRequest, Entity).

The xsd that schemagen is:

Client code snippet:

JAXBContext ctx1 = JAXBContext.newInstance( DdnRequest.class );
Marshaller m = ctx1.createMarshaller();
m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true );
m.marshal( req, new FileOutputStream( f ));
m.marshal( req, System.out );

ctx1 = JAXBContext.newInstance( "ddn:ddn.util" );
Unmarshaller um = ctx1.createUnmarshaller();
um.setSchema( getSchema("schema1.xsd"));
Object obj = um.unmarshal( f );
m.marshal( obj, System.out );
System.out.println( obj );
if ( obj instanceof DdnRequest )
DdnRequest r = (DdnRequest) obj;
System.out.println( "name: " + r.getName()
+ "\nid: " + r.getId() );
package ddn;

import java.util.*;

import javax.xml.bind.annotation.*;

public final class Entity {

public Entity() {
this.type = Entity.TYPE_TEMPLATE;
this.attributes = null;

private final String type;
private final Map attributes;

package ddn.util;

import java.util.Collection;
import java.util.LinkedHashSet;
import javax.xml.bind.annotation.*;

import ddn.Entity;

public class DdnRequest
private final Collection entities;
private String name = null;
private String id = null;

/** Creates a new instance of DdnRequest */
public DdnRequest()
entities = new LinkedHashSet();

public DdnRequest( String theName, String theId,
Collection theEntities )
name = theName;
id = theId;
if ( null != theEntities )
entities.addAll( theEntities );

I created jaxb.index and files.

schemagen deposits the 'schema1.xsd' in build/schemas.
Is the validator expected to find it there ?

For the unmarshaller, I tried with the alternate JAXBContext( Class c ) constructor as well.

I am wondering what I am doing wrong.

(BTW, I am building and running within NB4.1)

Help is appreciated.



Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2003-06-25

Accessors (or getters/setters ) are the same as properties.

Did you mean keeping fields and properties ?

Joined: 2004-05-13

I (apparently) did what you had suggested.

Here is the resulting xsd:

To achieve that I added:

@XmlRootElement(name = "DdnRequest")

And modified the fields to be 'protected'.

Joined: 2003-06-25

There are a few issues here.

But first, the posted code snippet don't compile. It would be really helpful to make sure they do before they are posted. It makes it easier and faster to get back with a resonse.

I tried the example (with changes to make the code fragment compile - some of which I had to guess) with an internal version of RI that I use and didn't encounter the problem you noted.

Here are some points to note:

The generated schema does not match the annotations. It is missing an global element declaration ddnRequest even though DdnRequest is annotated with @XmlRootElement. This is basic functionality which is working in many other cases. So please double check that the schema was generated after the @XmlRootElement annotation was added.

Note, a schema is not required for unmarshalling or marshalling, only for validation. The marshallng and unmarshalling is totally annotation driven. So the schemagen step can be omitted. Likewise you can also omit getSchema() , setSchema() calls for validation.

The method getSchema("schema1.xsd") is specific to you application. It is not clear what this is doing. But the following validation seems to work for me:


DdnRequest class only contains private fields. However, by default only public fields and Javabean properties are mapped by default.But DdnRequest with only private fields would not be mapped. Perhaps DdnRequest defined accessors/mutators that were omitted from the code snippet above ?

See javadoc for @javax.xml.bind.annotation.XmlAccessorType for default mapping fields and properties.

Joined: 2004-05-13

I will check the issues you mention.

A question that rises:

- I want the properties to be private, but accessors
to them to be public.

How do I go about achieving that ?


Joined: 2004-03-18

One way to do it is you can specify

package ddn;

import javax.xml.bind.annotation.XmlAccessorType;
import static javax.xml.bind.annotation.AccessType.PROPERTY;
in the

Alternatively you can annotate the top level class. See javadoc for javax.xml.bind.annotation.XmlAccessorType for details.