Java Reference
In-Depth Information
Generating
an
XML
Document
Instance
from
a
Schema
Problem
You want a programmatic way to get from an XML schema to an XML document instance.
Solution
There are, of course, tools that do this. You could use NetBeans 6 or XML Spy, for example,
to generate an XML document for you. But if you have a programmatic need to do the same
thing, or aren't using one of those tools, try the free Java tool XIG.
First, download XIG from Sourceforge at http://xml-xig.sourceforge.net . The current version
is xml-xig-0.1.1.
You need to pass XIG the name of the schema and the root element you want to create an
instance for. Here you are going to point to your Catalog schema and generate an XML docu-
ment based on the searchResults element. On a command prompt, run the JAR:
> java -jar xml-xig-0.1.1.jar "src/xml/META-INF/wsdl/Catalog.xsd"
search Catalog.xig
XIG: Generating Template src/xml/META-INF/wsdl/Catalog.xsd.xig...
XIG: Instantiating src/xml/META-INF/wsdl/Catalog.xsd.xml...
Two files are created: Catalog.xsd.xigand Catalog.xsd.xml. We'll look at the XIG file in a
moment.
Here is the generated Catalog.xsd.xmlfile:
<search>
<firstName>?{string}</firstName>
<lastName>?{string}</lastName>
</search>
You can then replace the ?{string} placeholders with your values.
Let's modify this source a little bit in order to run it through a validator to make sure that XIG
did its job properly. Let's put the namespace on it with a prefix. Here the Catalog.xsdis in the
same directory as my instance document:
<s:search xmlns:s="http://ns.soacookbook.com/catalog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ns.soacookbook.com/catalog Catalog.xsd">
Search WWH ::




Custom Search