aspose file tools*
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
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "xml error while reading it" Watch "xml error while reading it" New topic
Author

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="http://www.w3.org/2001/XMLSchema">
<xs:element name="contacts">
<xs:complexType>
<xs:sequence>
<xs:element ref="contact"/>
</xs:sequence>
</xs:complexType>
</xs:element>

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


my xml doc

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

<contacts xsi:noNamespaceSchemaLocation="contacts.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<contact>
<firstname>Edwin</firstname>
<lastname>Dankert</lastname>
</contact>
</contacts>

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 java.io.IOException;

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.transform.stream.StreamSource;
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 =
"http://java.sun.com/xml/jaxp/properties/schemaLanguage";

static final String W3C_XML_SCHEMA =
"http://www.w3.org/2001/XMLSchema";


public static void main(String[] args) {
try {
DocumentBuilderFactory lDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
// lDocumentBuilderFactory.setAttribute(JAXP_SCHEMA_LANGUAGE,W3C_XML_SCHEMA);
lDocumentBuilderFactory.setValidating(true);
lDocumentBuilderFactory.setNamespaceAware(true);
//lDocumentBuilderFactory.setFeature(XMLConstants.DEFAULT_NS_PREFIX,true);
//lDocumentBuilderFactory.setFeature(XMLConstants.RELAXNG_NS_URI,true);


SchemaFactory lSchemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
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) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
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: http://aspose.com/file-tools
 
subject: xml error while reading it