Well, I've almost done it. I've been trying to figure how to make the parser use my specific XSD, no matter if a particular xml document has specified where the XSD should be... If someone could provide some insight I would be deeply in debt to you...
Here's my snippet of code:
...
SAXParserFactory spfactory = SAXParserFactory.newInstance();
spfactory.setNamespaceAware(true);
spfactory.setValidating(true);
SAXParser saxParser = spfactory.newSAXParser();
xmlReader = saxParser.getXMLReader();
xmlReader.setFeature("http://apache.org/xml/features/validation/schema", true);
xmlReader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", (new File(this.xsd)).getAbsolutePath());
xmlReader.setContentHandler(handler);
xmlReader.setErrorHandler(handler);
xmlReader.parse(new InputSource(new StringReader(this.xml)));
...
If I comment out the line: xmlReader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", (new File(this.xsd)).getAbsolutePath()); everthing works fine and dandy, otherwise I get this nifty error:
org.xml.sax.SAXParseException: TargetNamespace.2: Expecting no namespace, but the schema document has a target namespace.
at org.apache.xerces.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1172)
at com.whateverittakes.templates.Template.validate(Template.java:96)
at com.whateverittakes.templates.Test.main(Test.java:42)
I've searched google for this particular error and I get 4 results, none which provide any useful information whatsoever.
One link I found suggests that by inserting that line of code, that I should be able to specify what xsd to use not matter what is specified in the schema location hints.
I'm developing with WSAD 5.1 and I'm just try to execute this code as a sample
java application. I had the WSAD generate the XSD and the XML for me and I must admit, I'm no pro when it comes to XML. Here's the XSD WSAD generated for me:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
targetNamespace="http://www.whateverittakes.com/TEMPLATE/ABC"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:Q1="xs" xmlns:AFITESTTEMPLATE="http://www.blah.com/TEMPLATE/ABC">
<xsd:element name="order" type="ABCTESTTEMPLATE
rder"/>
<xsd:complexType name="order">
<xsd:sequence>
<xsd:element name="renter" type="xsd:boolean" minOccurs="0" maxOccurs="1" />
<xsd:element name="childage" type="xsd:string" minOccurs="0" maxOccurs="1" />
<xsd:element name="income" type="xsd:string" minOccurs="0" maxOccurs="1" />
<xsd:element name="language" type="xsd:string" minOccurs="0" maxOccurs="1" />
<xsd:element name="homevalue" type="xsd:string" minOccurs="0" maxOccurs="1" />
<xsd:element name="polkapp" type="xsd:string" minOccurs="0" maxOccurs="1" />
<xsd:element name="carrierroute" type="xsd:string" minOccurs="0" maxOccurs="1" />
<xsd:element name="phoneonly" type="xsd:boolean" minOccurs="0" maxOccurs="1" />
<xsd:element name="zipcodes" type="xsd:string" minOccurs="1" maxOccurs="1" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
and here's a sample XML
doc:
<?xml version="1.0" encoding="UTF-8"?>
<ABCTESTTEMPLATE
rder
xmlns:ABCTESTTEMPLATE="http://www.blah.com/TEMPLATE/ABC"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.blah.com/TEMPLATE/ABC Order.xsd ">
<renter>true</renter>
<childage>44</childage>
<income>33000</income>
<language>english</language>
<homevalue>150000</homevalue>
<polkapp>ok</polkapp>
<carrierroute>ok</carrierroute>
<phoneonly>true</phoneonly>
<zipcodes>61704</zipcodes>
</ABCTESTTEMPLATE
rder>
Again, any insight would be much appreciated. Thanks, Dave