File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes xml error while reading it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "xml error while reading it" Watch "xml error while reading it" New topic

xml error while reading it

anish reddy
Ranch Hand

Joined: Apr 19, 2008
Posts: 74
my schema
<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="">
<xs:element name="contacts">
<xs:element ref="contact"/>

<xs:element name="contact">
<xs:element name="firstname" type="xs:NCName"/>
<xs:element name="lastname" type="xs:NCName"/>

my xml doc

<?xml version="1.1" encoding="UTF-8"?>

<contacts xsi:noNamespaceSchemaLocation="contacts.xsd"

my java dom program

* Generated/ at : 09-May-2006 20:45:32
* Copyright (c) 2005 - 2006, Edwin Dankert
* All rights reserved.

package com.edankert.dom;


import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.validation.SchemaFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.edankert.SimpleErrorHandler;

public class ValidateExternalSchema {
static final String JAXP_SCHEMA_LANGUAGE =

static final String W3C_XML_SCHEMA =

public static void main(String[] args) {
try {
DocumentBuilderFactory lDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
// lDocumentBuilderFactory.setAttribute(JAXP_SCHEMA_LANGUAGE,W3C_XML_SCHEMA);

SchemaFactory lSchemaFactory = SchemaFactory.newInstance("");
lDocumentBuilderFactory.setSchema(lSchemaFactory.newSchema(new Source[] {new StreamSource("contacts.xsd")}));

DocumentBuilder lDocumentBuilder = lDocumentBuilderFactory.newDocumentBuilder();
lDocumentBuilder.setErrorHandler(new SimpleErrorHandler());
lDocumentBuilder.parse(new InputSource("contacts.xml"));

} catch (ParserConfigurationException e) {
} catch (SAXException e) {
} catch (IOException e) {
error iam getting

Document is invalid: no grammar found.
Document root element "contacts", must match DOCTYPE root "null

please any one help regarding that

I agree. Here's the link:
subject: xml error while reading it
It's not a secret anymore!