• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JAXB Validation Error

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Iam using JAXB. I get an validation error. When i try to marshal an object to XML. The code is simple as shown below

1. Container.xsd
-----------------

<?xml version="1.0" encoding="UTF-8"?>
<schema targetNamespace="http://agi.com"
xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://agi.com"
elementFormDefault="qualified">

<complexType name="ContainerType" abstract="true">
<sequence>
<choice>
<element name="File" type="anyType" />

<element name="Pen" type="anyType" />
</choice>
</sequence>
</complexType>
<element name="Container" type="tns:ContainerType" abstract="true" />

</schema>

2. Holder.xsd
---------------

<?xml version="1.0" encoding="UTF-8"?>
<schema targetNamespace="http://agi.com"
xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://agi.com"
elementFormDefault="qualified">

<include schemaLocation="Container.xsd"></include>

<complexType name="HolderType">
<complexContent>
<restriction base="tns:ContainerType">
<sequence>
<choice>
<element name="File">
<complexType>
<sequence>
<element name="FileName"
type="string">
</element>
<element name="Capacity"
type="integer">
</element>
</sequence>
</complexType>
</element>
<element name="Pen">
<complexType>
<sequence>
<element name="Make"
type="string">
</element>
</sequence>
</complexType>
</element>
</choice>
</sequence>
</restriction>
</complexContent>
</complexType>

<element name="Holder" type="tns:HolderType"></element>
</schema>

---------------------
Main.java
---------------------

package test.client;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.math.BigInteger;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.Validator;

import test.vo.AnyType;
import test.vo.Holder;
import test.vo.ObjectFactory;
import test.vo.HolderType.FileType;
import test.vo.HolderType.PenType;

import com.sun.xml.bind.StringInputStream;

public class Main {
public static void main(String s[]) throws Exception{

JAXBContext ctx = JAXBContext.newInstance("test.vo" );

ObjectFactory obj = new ObjectFactory();
Holder input = obj.createHolder();

FileType file = obj.createHolderTypeFileType();

file.setCapacity( BigInteger.valueOf(35) );
file.setFileName("Accounts");

AnyType any = obj.createAnyType();
List lst = any.getContent();
lst.add(file);

input.setFile(any);


////////////////////////////

Marshaller marsh = ctx.createMarshaller();
marsh.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
new Boolean(true));

marsh.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
"http://agi.com");

Validator validator = ctx.createValidator();
validator.validate(input);



StringWriter sw = new StringWriter();
marsh.marshal(input, new PrintWriter(sw) );
StringBuffer sb = sw.getBuffer();

System.out.println(sb.toString() );


}
}


This throws the following exception
-----------------------------------------------------------
DefaultValidationEventHandler: [ERROR]: tag name "test.vo.AnyType" is not allowed. Possible tag names are: <test.vo.HolderType.FileType>
Location: obj: test.vo.impl.HolderImpl@1cd8669
Exception in thread "main" com.sun.xml.bind.serializer.AbortSerializationException: tag name "test.vo.AnyType" is not allowed. Possible tag names are: <test.vo.HolderType.FileType>
at test.vo.impl.runtime.ValidationContext.reportEvent(ValidationContext.java:199)
at test.vo.impl.runtime.ValidationContext.reportEvent(ValidationContext.java:166)
at test.vo.impl.runtime.MSVValidator.childAsElementBody(MSVValidator.java:336)
at test.vo.impl.runtime.MSVValidator.childAsBody(MSVValidator.java:292)
at test.vo.impl.ContainerTypeImpl.serializeBody(ContainerTypeImpl.java:52)
at test.vo.impl.HolderTypeImpl.serializeBody(HolderTypeImpl.java:30)
at test.vo.impl.HolderImpl.serializeBody(HolderImpl.java:43)
at test.vo.impl.runtime.MSVValidator._validate(MSVValidator.java:102)
at test.vo.impl.runtime.MSVValidator.validate(MSVValidator.java:77)
at test.vo.impl.runtime.ValidationContext.validate(ValidationContext.java:75)
at test.vo.impl.runtime.ValidatorImpl.validate(ValidatorImpl.java:121)
at test.vo.impl.runtime.ValidatorImpl.validate(ValidatorImpl.java:104)
at test.client.Main.main(Main.java:63)

-----------------------------------------------

However if i commentout the validation it works fine yeilding the
following result

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Holder xsi:schemaLocation="http://agi.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://agi.com">
<File>
<FileName>Accounts</FileName>
<Capacity>35</Capacity>
</File>
</Holder>

Which look good conforming to the schema above

It willbe helpful if someout could point out the mistake iam doing

Please help

Regards
Agilan Palani
 
He baked a muffin that stole my car! And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic