Steve Dussinger
Usenet Archive
- Joined
- Jul 21, 2001
- Messages
- 9
- Reaction score
- 0
Can anyone tell me just what I'm doing wrong? I've got a simple java program using Xerces 1.4.2 to parse a simple XML file. I want to use schema validation, so I turned on the appropriate features, but no matter what I do I get errors saying that none of my elements are defined. I've tried everything I can think of, but I still can't get it to work.
Here's the code I'm using:
import org.apache.xerces.dom.DocumentImpl;
import org.apache.xerces.parsers.DOMParser;
import org.apache.xerces.validators.schema.XUtil;
import org.w3c.dom.*;
import org.xml.sax.*;
import java.io.*;
public class parseTest implements ErrorHandler
{
public void error(SAXParseException se) throws SAXParseException { System.out.println ("ERROR: " + se.getMessage ); System.out.println("ERROR : Line Number =" + se.getLineNumber); System.out.println("ERROR : Col Number =" + se.getColumnNumber); }
public void warning(SAXParseException se)
{
System.out.println ("WARNING: " + se.getMessage ); }
public void fatalError (SAXParseException se) throws SAXParseException { System.out.println ("FATAL: " + se.getMessage ); }
private void test (String fileName)
{
try {
System.out.println ("Parsing: " + fileName);
DOMParser parser = new DOMParser ;
parser.setFeature(
"http://apache.org/xml/features/dom/defer-node-expansion", true); parser.setFeature( "http://xml.org/sax/features/validation", true ); parser.setFeature( "http://xml.org/sax/features/namespaces", true ); parser.setFeature( "http://apache.org/xml/features/validation/schema", true ); parser.setFeature( "http://apache.org/xml/features/dom/include-ignorable-whitespace", false);
parser.setErrorHandler (this);
parser.parse (new InputSource (new FileInputStream (fileName)));
} catch (Exception e) {
e.printStackTrace ;
}
}
public static void main (String args)
{
new parseTest .test (args[0]);
}
}
And the XML:
<?xml version="1.0" ?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema"
xsi:noNamespaceSchemaLocation="schemaTest.xsd">
<row>
<product>Product</product>
</row>
</root>
And the XSD:
<?xml version ="1.0"?>
<xsd:schema xmlns:xsd = "http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="rowType" content = "elementOnly"> <xsd:sequence> <xsd:element name = "product" type="string"/> </xsd:sequence> </xsd:complexType>
<xsd:complexType name="rootType" content = "elementOnly"> <xsd:sequence> <xsd:element name = "row" type="rowType"/> </xsd:sequence> </xsd:complexType>
<xsd:element name = "root" type="rootType"/>
</xsd:schema>
When I run the Java program, I get the following:
ERROR: Element type "root" must be declared.
ERROR : Line Number =3
ERROR : Col Number =100
ERROR: Element type "row" must be declared.
ERROR : Line Number =4
ERROR : Col Number =8
ERROR: Element type "product" must be declared.
ERROR : Line Number =5
ERROR : Col Number =14
These errors come from my error handler code, so I know that it's installed correctly...
I've tried changing the xsi:noNameSpaceSchemaLocation to point to a file which doesn't exist, expecting some kind of error telling me that Xerces can't find the file. No such luck, it keeps giving me the same errors. I'm convinced that Xerces isn't even trying to look at the schema file, but I have no idea why. Does anyone out there have a clue what I'm doing wrong??
Any help is greatly appreciated...
Thanx,
Steve
Here's the code I'm using:
import org.apache.xerces.dom.DocumentImpl;
import org.apache.xerces.parsers.DOMParser;
import org.apache.xerces.validators.schema.XUtil;
import org.w3c.dom.*;
import org.xml.sax.*;
import java.io.*;
public class parseTest implements ErrorHandler
{
public void error(SAXParseException se) throws SAXParseException { System.out.println ("ERROR: " + se.getMessage ); System.out.println("ERROR : Line Number =" + se.getLineNumber); System.out.println("ERROR : Col Number =" + se.getColumnNumber); }
public void warning(SAXParseException se)
{
System.out.println ("WARNING: " + se.getMessage ); }
public void fatalError (SAXParseException se) throws SAXParseException { System.out.println ("FATAL: " + se.getMessage ); }
private void test (String fileName)
{
try {
System.out.println ("Parsing: " + fileName);
DOMParser parser = new DOMParser ;
parser.setFeature(
"http://apache.org/xml/features/dom/defer-node-expansion", true); parser.setFeature( "http://xml.org/sax/features/validation", true ); parser.setFeature( "http://xml.org/sax/features/namespaces", true ); parser.setFeature( "http://apache.org/xml/features/validation/schema", true ); parser.setFeature( "http://apache.org/xml/features/dom/include-ignorable-whitespace", false);
parser.setErrorHandler (this);
parser.parse (new InputSource (new FileInputStream (fileName)));
} catch (Exception e) {
e.printStackTrace ;
}
}
public static void main (String args)
{
new parseTest .test (args[0]);
}
}
And the XML:
<?xml version="1.0" ?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema"
xsi:noNamespaceSchemaLocation="schemaTest.xsd">
<row>
<product>Product</product>
</row>
</root>
And the XSD:
<?xml version ="1.0"?>
<xsd:schema xmlns:xsd = "http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="rowType" content = "elementOnly"> <xsd:sequence> <xsd:element name = "product" type="string"/> </xsd:sequence> </xsd:complexType>
<xsd:complexType name="rootType" content = "elementOnly"> <xsd:sequence> <xsd:element name = "row" type="rowType"/> </xsd:sequence> </xsd:complexType>
<xsd:element name = "root" type="rootType"/>
</xsd:schema>
When I run the Java program, I get the following:
ERROR: Element type "root" must be declared.
ERROR : Line Number =3
ERROR : Col Number =100
ERROR: Element type "row" must be declared.
ERROR : Line Number =4
ERROR : Col Number =8
ERROR: Element type "product" must be declared.
ERROR : Line Number =5
ERROR : Col Number =14
These errors come from my error handler code, so I know that it's installed correctly...
I've tried changing the xsi:noNameSpaceSchemaLocation to point to a file which doesn't exist, expecting some kind of error telling me that Xerces can't find the file. No such luck, it keeps giving me the same errors. I'm convinced that Xerces isn't even trying to look at the schema file, but I have no idea why. Does anyone out there have a clue what I'm doing wrong??
Any help is greatly appreciated...
Thanx,
Steve