Validating and creating a XOM document using a default schema
Hi people. I'm having a problem that I really can't solve no matter how much time I spend on it and was wondering if it is possible.
I want to validate and create XOM documents. The xml will always be a certain format whether the schema is defined in the xml or not. I need to handle both cases.
When the xml has this in its first element:
<?xml version="1.0" ?>
<profile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="cardprofile3G.xsd" name="chineseCard" type="3G">
...
..
etc.
It works. But when I have this in the xml:
<?xml version="1.0" ?>
<profile name="chineseCard" type="3G">
...
..
etc.
It does not work. I get the exception:
nu.xom.ValidityException: cvc-elt.1: Cannot find the declaration of element 'profile'. at line 2, column 39
at nu.xom.Builder$ValidityRequired.error(Builder.java:1227)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1887)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:685)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:400)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:626)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3084)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:912)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:645)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:508)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at nu.xom.Builder.build(Builder.java:1127)
at nu.xom.Builder.build(Builder.java:586)
This is my code:
//InputStream inputStream = new FileInputStream(new File("C:\\Documents and Settings\\user\\My Documents\\chineseCard.xml"));
InputStream inputStream =new FileInputStream(new File("C:\\Documents and Settings\\user\\My Documents\\chineseCardNoSchema.xml"));
EntityResolver entityResolver =new ResourceEntityResolver("cardprofile3G.xsd","/com/user/resources/cardprofiler/cardprofile3G.xsd");
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setNamespaceAware(true);
XMLReader reader = saxParserFactory.newSAXParser().getXMLReader();
reader.setEntityResolver(entityResolver);
reader.setFeature("http://apache.org/xml/features/validation/schema",true);
Builder parser =new Builder(reader,true);
Document document = parser.build(inputStream);
Is there anyway I can check to see if a schema isn't defined in the xml and then add one if required?
I'm really stuck and would appreciate any help!
Thanks, Boomah.
By the way, I'm using Java 6 and XOM 1.1
Message was edited by:
Boomah

