With the work-around described below, is the sample schema the source schema to be processed by jaxb? If so, this work-around is problematic for me, because the schema in question is an industry-standard schema which we include, but do not modify.
Date: Wed, 18 Feb 2004 15:25:41 -0500 From: Ryan Shoemaker - JavaSoft East <Ryan.Shoemaker@Sun.COM> Content-type: multipart/mixed; boundary=------------050803000901090603000006 Subject: pattern in a simple type
oro7d3@netscape.net wrote:
> Hi,
>
> I have a question in jaxb. I have this simple type attribute with a pattern value "0" or "1". I have another element which reference this attribute.
> JAXB creates a setMushUnderstand( boolean value); for me.
> But when it marshalls the object, it marshalls 'mustUnderstand="false"' instead of using '0' or '1'.
>
> Is there a way to configure JAXB to marshall either "0" for true, "1" for false?
>
Yes, you just need to specify your own print and parse methods.
Take a look at the attached sample for more detail.
Once you have this all wired up, you should be able to do some-
thing like:
org.acme.foo.Foo f = (new org.acme.foo.ObjectFactory()).createFoo();
f.setValue("blarg");
f.setMyAttr(true);
m.marshal(f, System.out);
and see output like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<foo myAttr="1">blarg</foo>
Thanks,
--Ryan
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
jxb:version="1.0">
<xsd:annotation>
<xsd:appinfo>
<jxb:schemaBindings>
<jxb:package name="org.acme.foo"/>
</jxb:schemaBindings>
</xsd:appinfo>
</xsd:annotation>
<xsd:element name="foo">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute ref="myAttr"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
<xsd:attribute name="myAttr">
<xsd:simpleType>
<xsd:annotation>
<xsd:appinfo>
<jxb:javaType name="boolean"
parseMethod="org.acme.foo.Converter.parseBoolean"
printMethod="org.acme.foo.Converter.printBoolean" />
</xsd:appinfo>
</xsd:annotation>
<xsd:restriction base="xsd:boolean">
<xsd:pattern value="0|1"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:schema>
package org.acme.foo;
public class Converter {
public static boolean parseBoolean( String lexicalBoolean ) {
return "1".equals(lexicalBoolean) ? true : false;
}
public static String printBoolean( boolean booleanValue ) {
return booleanValue ? "1" : "0";
}
}
___________________________________________________________
Sign-up for Ads Free at Mail.com
http://www.mail.com/?sr=signup