Hi,
I'm really new to xmlbeans and I have a question. I have 2 xsd files which
have pretty much the same element names. The only difference is that they
have some complex types which have different elements inside them but have
same global element name, e.g.:
Books.xsd would have a complex type called ItemProperties:
<xs:element minOccurs="0" name="ItemProperties">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="ISBN"
nillable="true" type="xs:string" />
<xs:element minOccurs="0" name="Author"
nillable="true" type="xs:string" />
<xs:element minOccurs="0" name="of-Pages"
nillable="true" type="xs:string" />
<xs:element minOccurs="0" name="Series-Vol."
nillable="true" type="xs:string" />
<xs:element minOccurs="0" name="Publisher"
nillable="true" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
Apparel.xsd would also have a complex type call ItemProperties:
<xs:element minOccurs="0" name="ItemProperties">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="Style"
nillable="true" type="Style_Type" />
<xs:element minOccurs="0" name="Womens-Sizes"
nillable="true" type="Womens-Sizes_Type" />
<xs:element minOccurs="0" name="Sizes"
nillable="true" type="Sizes_Type" />
<xs:element minOccurs="0"
name="Infant-Child-sizes" nillable="true" type="Infant-Child-sizes_Type" />
<xs:element minOccurs="0" name="Juniors-Sizes"
nillable="true" type="Juniors-Sizes_Type" />
</xs:sequence>
</xs:complexType>
</xs:element>
They both have the same namespace and I can't change them because that is
given to me:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="http://www.mysite.com/XMLSchema"
elementFormDefault="qualified" targetNamespace="http://www.
mysite.com/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema">
The wiki said to use multiple class loaders for each jar to solve this issue
of same namespace and element names. So I compiled them into .jar with scomp
individually since sfactor would give me an error and I think the error is
because there are complex types with same name but different internal
structure?
I found this sample code from the mailing list back in 2005:
// get a SchemaTypeLoader from an array of directories or jar
files
File[] schemaPath = null;
SchemaTypeLoader stl =
XmlBeans.typeLoaderForResource(XmlBeans.resourceLoaderForPath(schemaPath
));
// include the built-in type system
stl = XmlBeans.typeLoaderUnion(new
SchemaTypeLoader[]{XmlBeans.getBuiltinTypeSystem(), stl});
// parse the document in the given
a.b.c.RETURNDATADocument doc =
(a.b.c.RETURNDATADocument)stl.parse("xml", null, null);
// or create a new one, given a schemaType
SchemaType st = stl.findDocumentType(new QName("a.b.c",
"RETURNDATA"));
a.b.c.RETURNDATADocument newDoc =
(a.b.c.RETURNDATADocument)stl.newInstance(st, null);
I tried to compile each xsd with the following xsdconfig file:
For books.xsd:
<xb:config xmlns:xb="http://xml.apache.org/xmlbeans/2004/02/xbean/config">
<xb:namespace uri="http://www.mysite.com/XMLSchema">
<xb:package>com.mysite.XMLSchema.Book</xb:package>
</xb:namespace>
</xb:config>
For apparel.xsd:
<xb:config xmlns:xb="http://xml.apache.org/xmlbeans/2004/02/xbean/config">
<xb:namespace uri="http://www.mysite.com/XMLSchema">
<xb:package>com.mysite.XMLSchema.Apparel</xb:package>
</xb:namespace>
</xb:config>
And this is my source code:
import java.io.File;
import javax.xml.namespace.QName;
import org.apache.xmlbeans.SchemaType;
import org.apache.xmlbeans.SchemaTypeLoader;
import org.apache.xmlbeans.XmlBeans;
import org.apache.xmlbeans.XmlException;
import com.mysite.XMLSchema.Apparel.ItemDataFeedDocument;
public class Test {
/**
* @param args
* @throws XmlException
*/
public static void main(String[] args) {
File[] schemaPath = {new File("Apparel.jar"), new
File("Books.jar")};
SchemaTypeLoader stl =
XmlBeans.typeLoaderForResource(XmlBeans.resourceLoaderForPath(schemaPath));
stl = XmlBeans.typeLoaderUnion(new
SchemaTypeLoader[]{XmlBeans.getBuiltinTypeSystem(), stl});
SchemaType st = stl.findDocumentType(new
QName("com.mysite.XMLSchema.Apparel", "ItemDataFeed"));
com.mysite.XMLSchema.Apparel.ItemDataFeedDocument newDoc1 =
(com.mysite.XMLSchema.Apparel.ItemDataFeedDocument)stl.newInstance(st,
null);
com.mysite.XMLSchema.Accessories.ItemDataFeedDocument newDoc2 =
(com.mysite.XMLSchema.Accessories.ItemDataFeedDocument)stl.newInstance(st,
null);
com.mysite.XMLSchema.Apparel.ItemDataFeedDocument.ItemDataFeed
apparel = newDoc1.addNewItemDataFeed();
com.mysite.XMLSchema.Apparel.ItemDataFeedDocument.ItemDataFeed.Items
apparelItems = apparel.addNewItems();
com.mysite.XMLSchema.Apparel.ItemDataFeedDocument.ItemDataFeed.Items.Item
aItem = apparelItems.addNewItem();
com.mysite.XMLSchema.Apparel.ItemDataFeedDocument.ItemDataFeed.Items.Item.It
emProperties itemProp = aItem.addNewItemProperties();
itemProp.setAge(com.mysite.XMLSchema.Apparel.AgeType.TEEN);
System.out.println(newDoc1.toString());
}
}
But SchemaType st would just return null? What am I doing wrong and is there
a better solution to the problem? I just want to be able to create new
instances of the xmlbean classes that have the same namespace and element
name (but different internal structures for the complex types). Then set
some values for the elements and write to file.
Any help is greatly appreciated.
John