Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

XML validation using DOM

11 views
Skip to first unread message

vacc...@gmail.com

unread,
Apr 12, 2007, 3:26:16 PM4/12/07
to
Hi... i am new to XML validation using XML...
I am trying to Validate the XML document using XMLSchema.. The code
i am using is ...but its giving Error like
could any one tell me where i went wrong.....
Error:
ValidationExample.java:27: cannot resolve symbol
symbol : variable XMLConstants
location: class ValidationExample
SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
^
ValidationExample.java:33: cannot resolve symbol
symbol : method Validate (org.w3c.dom.Document)
location: class javax.xml.validation.Validator
valid.Validate(doc);
^
Programme:

import java.io.*;
import javax.xml.validation.*;
import org.xml.sax.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.*;
import javax.xml.parsers.*;
import java.lang.*;
import org.w3c.dom.*;

public class ValidationExample{
public static void main (String args[]){
//parse the XML document in to XML
try {

// DOMParser parser = new DOMParser();
// parser.parse("DEMMODTD.xml"));
// Document doc = parser.getDocument();

File docfile = new File("DEMODTD.XML");
Document doc = null;
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(docfile);
dbf.setValidating(true);

// set the Schema Factory
SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source schemaFile = new StreamSource(new
File("DEMODTD.xsd"));
Schema schema = factory.newSchema(schemaFile);
Validator valid = schema.newValidator();
//validate the DOm tree

valid.Validate(doc);
}catch (SAXException e) {
System.out.println("error"+e.getMessage());
}catch (IOException e){
System.out.println("error"+e.getMessage());
}catch (ParserConfigurationException e) {
System.out.println("error"+e.getMessage());
}catch (FactoryConfigurationError e) {
System.out.println("error"+e.getMessage());
}

}
}

Joshua Cranmer

unread,
Apr 12, 2007, 5:58:39 PM4/12/07
to
vacc...@gmail.com wrote:
> Hi... i am new to XML validation using XML...
> I am trying to Validate the XML document using XMLSchema.. The code
> i am using is ...but its giving Error like
> could any one tell me where i went wrong.....
> Error:
> ValidationExample.java:27: cannot resolve symbol
> symbol : variable XMLConstants
The compiler can't find the class XMLConstants.

> ValidationExample.java:33: cannot resolve symbol
> symbol : method Validate (org.w3c.dom.Document)
> location: class javax.xml.validation.Validator
> valid.Validate(doc);

do you mean valid.validate(doc)?

gg

unread,
May 21, 2007, 8:44:35 PM5/21/07
to

<vacc...@gmail.com> wrote in message
news:1176405976....@l77g2000hsb.googlegroups.com...

> Hi... i am new to XML validation using XML...
> I am trying to Validate the XML document using XMLSchema.. The code
> i am using is ...but its giving Error like
> could any one tell me where i went wrong.....
> Error:
> ValidationExample.java:27: cannot resolve symbol
> symbol : variable XMLConstants
> location: class ValidationExample
> SchemaFactory factory =
> SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Try
SchemaFactory.newInstance
(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);

> ^
> ValidationExample.java:33: cannot resolve symbol
> symbol : method Validate (org.w3c.dom.Document)
> location: class javax.xml.validation.Validator
> valid.Validate(doc);
> ^

what if you try
Validator validator = schema.newValidator();
//validate the DOM tree

///valid.validate(doc);
try {
validator.validate(new DOMSource(document));
} catch (SAXException e) {
// instance document is invalid!
// your code to handel this here
}


0 new messages