Java Reference
In-Depth Information
Validating
an
XML
Document
Against
Multiple
Schemas
Problem
You have an XML document instance that uses elements from more than one namespace and
you need to validate it.
Solution
Using the code from the last recipe, simply create a string array of the names of the schemas
you need, and pass that to your DocumentBuilderFactory .
Say you have the following XML document that is defined by multiple schemas:
<c:customer cid="99" xmlns:c="urn:ns:soacookbook:customer">
<gen:name xmlns:gen="urn:ns:soacookbook:general">
Indiana Jones
</gen:name>
<a:address xmlns:a="urn:ns:soacookbook:address">
<a:street>1212 Some Street</a:street>
<a:city>Washington,DC</a:city>
<a:state>VA</a:state>
</a:address>
</c:customer>
This is more what we're accustomed to in the real world. Validating such a document is almost
exactly the same as validating a document with a single schema. But in this case, you pass
the DocumentBuilderFactory an array of strings representing the names of the schemas you
need:
...
//create a string path to each schema needed for Customer.xml
String schemaAddress = ROOT + "a.xsd";
String schemaGeneral = ROOT + "gen.xsd";
String schemaCustomer = ROOT + "Customer.xsd";
String[] schemas =
{schemaAddress, schemaGeneral, schemaCustomer};
//change this method to accept vararg schema
Search WWH ::




Custom Search