This is no expert. How about moving the customer and name definitions, beore they are referenced? Just something to try, I haven't ever used JAXP.

Swapna

From: "Yang, Jeffrey" <[EMAIL PROTECTED]>
Reply-To: [EMAIL PROTECTED]
To: "'[EMAIL PROTECTED]'" <[EMAIL PROTECTED]>
Subject: RE: Creating XSModel/Grammer from dom element
Date: Thu, 18 Dec 2003 21:39:11 -0000

I am having problems with validating an xml file against a schema, using JAXP.
The problems are:
1. I get errors saying that the elements in my xml file are NOT declared.
2. it does not validate. even with an invalid (but well-formed) xml file, it
says it is valid, but it still prints the following messages:
3. if I used dtd instead of schema, then the same java code works fine.


---- error message ----
error: org.xml.sax.SAXParseException: Element type "customers" must be
declared.
error: org.xml.sax.SAXParseException: Element type "customer" must be
declared.
error: org.xml.sax.SAXParseException: Element type "name" must be declared.
---- end of error message ----


I wonder what is missing in my code. Here are the schema, the xml file and the
java code


---- schema ----
<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema";>
<xsd:element name="customers">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element name="customer" ref="customer" maxOccurs="unbounded" />
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>

<xsd:element name="customer">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element name="name" ref="name" />
    </xsd:sequence>
    <xsd:attribute name="id" type="xsd:int" use="required" />
  </xsd:complexType>
</xsd:element>

<xsd:element name="name" type="xsd:string" />

</xsd:schema>

---- end of schema ----

---- xml ----
<?xml version="1.0" encoding="UTF-8"?>
<customers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:noNamespaceSchemaLocation="customer.xsd">
  <customer id="1">
    <name>jeff yang</name>
  </customer>
</customers>
---- end of xml ----

---- java code ----
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;

public class JaxpParseXml3 implements ErrorHandler
{
  private String propertyFilename = null;
  private String fileDir = null;
  private String dataFilename = null;
  private boolean isValid = true;

  public static void main(String [] args)
  {
    JaxpParseXml3 px = new JaxpParseXml3();
    px.parseDocument1();
  }

public void parseDocument1()
{
try
{
System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
"org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
DocumentBuilderFactory DBfactory = DocumentBuilderFactory.newInstance();
DBfactory.setValidating(true);
DocumentBuilder parser = DBfactory.newDocumentBuilder();
parser.setErrorHandler(this);
Document doc = parser.parse(new InputSource(new
java.io.FileInputStream("customers.xml")));
if (isValid) System.out.println("document is valid");
else System.out.println("document is INVALID");
}
catch(Exception e)
{
System.out.println(e);
System.out.println("document is INVALID. process is aborted");
}
}


  public void error(SAXParseException e) throws SAXException
  {
    System.out.println("error: " + e);
    isValid = false;
  }

  public void fatalError(SAXParseException e) throws SAXException
  {
    System.out.println("FATAL error: " + e);
    isValid = false;
  }

  public void warning(SAXParseException e) throws SAXException
  {
    System.out.println("warning: " + e);
  }
} //class---- end of java code ----

---- portion of my classpath ----
c:\mylib\java\xerces.jar;c:\mylib\java\jdom.jar;c:\mylib\java\jaxp.jar
---- end of classpath ----


Thanks very much for your help.


Jeff

==============================================================================
This message is for the sole use of the intended recipient. If you received
this message in error please delete it and notify us. If this message was
misdirected, CSFB does not waive any confidentiality or privilege. CSFB
retains and monitors electronic communications sent through its network.
Instructions transmitted over this system are not binding on CSFB until they
are confirmed by us. Message transmission is not guaranteed to be secure.
==============================================================================



--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]


_________________________________________________________________
Add glamour to your desktop. Let your screen sizzle. http://server1.msn.co.in/msnchannels/Entertainment/wallpaperhome.asp Download the hottest wallpapers.



--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to