M
marco
Ok, I start here a new post, because I ran into some more complex
problems .
I got an XML schema, binded via xjc.sh:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="1.0">
<xs:element name="Resource" type="MyResource"/>
<xs:complexType name="MyResource">
<xs:sequence minOccurs="1" maxOccurs="unbounded">
<xs:element name="FirstTag" type="xs:string"/>
<xs:element name="SecondTag" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
and a XML Document:
<?xml version="1.0" encoding="utf-8"?>
<Resource xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="newschema.xsd">
<FirstTag>MyFirstValue</FirstTag>
<SecondTag>MySecondValue</SecondTag>
</Resource>
Following the API :
1 ObjectFactory obj = new ObjectFactory();
2 JAXBContext jc = JAXBContext.newInstance( "newschema" );
3 Unmarshaller u = jc.createUnmarshaller();
4 MyResource r = (MyResource)u.unmarshal(new File( "template.xml" ));
5 Marshaller m = jc.createMarshaller();
6 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
7 OutputStream os = new FileOutputStream( "newtempl.xml" );
8 m.marshal( r, os );
The result is a ClassCastException.
Replacing line 4 and 8 with:
4b Object o = u.unmarshal( new File( "template.xml" ) );
8b m.marshal (o, os);
all works fine.
So, my question are:
1) How to cast the object o?
2) Since the setValidation(true) is deprecated, how to validate?
Thanks a lot!
problems .
I got an XML schema, binded via xjc.sh:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="1.0">
<xs:element name="Resource" type="MyResource"/>
<xs:complexType name="MyResource">
<xs:sequence minOccurs="1" maxOccurs="unbounded">
<xs:element name="FirstTag" type="xs:string"/>
<xs:element name="SecondTag" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
and a XML Document:
<?xml version="1.0" encoding="utf-8"?>
<Resource xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="newschema.xsd">
<FirstTag>MyFirstValue</FirstTag>
<SecondTag>MySecondValue</SecondTag>
</Resource>
Following the API :
1 ObjectFactory obj = new ObjectFactory();
2 JAXBContext jc = JAXBContext.newInstance( "newschema" );
3 Unmarshaller u = jc.createUnmarshaller();
4 MyResource r = (MyResource)u.unmarshal(new File( "template.xml" ));
5 Marshaller m = jc.createMarshaller();
6 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
7 OutputStream os = new FileOutputStream( "newtempl.xml" );
8 m.marshal( r, os );
The result is a ClassCastException.
Replacing line 4 and 8 with:
4b Object o = u.unmarshal( new File( "template.xml" ) );
8b m.marshal (o, os);
all works fine.
So, my question are:
1) How to cast the object o?
2) Since the setValidation(true) is deprecated, how to validate?
Thanks a lot!