views:

59

answers:

2
+2  A: 

What have you tried? Googling "validate xml against schema java" turns up some pretty detailed instructions, including this which gives code examples, and this tutorial

Paul
I tried with the code mentioned already... I am getting the error as output when run..
What code have you tried? And what error do you get? You've not told us about any errors.
Paul
HiERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2JDWP exit error AGENT_ERROR_NO_JNI_ENV(183): [../../../src/share/back/util.c:820]
Googling for that error suggests the problem is some misconfiguration in your environment, nothing to do with the actual validation code. If you want help, you need to post the actual code and more details of the environment you are using.
Paul
i got the solution from the examples given..
Thanks for the links.But i am manualyy giving the XML file and the schema in a string...
try { SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); spf.setSchema(sf.newSchema(new SAXSource(new InputSource(new StringReader(schemaString))))); parser = spf.newSAXParser(); } catch(SAXException e) { e.printStackTrace(System.err); System.exit(1); } catch(ParserConfigurationException e) { e.printStackTrace(System.err); System.exit(1); }
MySAXHandler handler = new MySAXHandler(); System.out.println(schemaString); parser.parse(new InputSource(new StringReader(xmlString)), handler); } static String xmlString = "<?xml version=\"1.0\"?>" + "<birthdate>" + "<month>January</month>" + "<day>21</day>" + "<year>1983</year>" + "</birthdate>";
static String schemaString ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<xs:element name=\"birthdate\">" + "<xs:complexType>" + "<xs:sequence>" + "<xs:element name=\"month\" type=\"xs:string\"/>" + "<xs:element name=\"day\" type=\"xs:int\"/>" + "<xs:element name=\"year\" type=\"xs:int\" />" + "</xs:sequence>" + "</xs:complexType>" + "</xs:element>" + "</xs:schema>";}
class MySAXHandler extends DefaultHandler { public void startDocument() { System.out.println("Start document: "); } public void endDocument() { System.out.println("End document: "); } public void startElement(String uri, String localName, String qname, Attributes attr) { System.out.println("Start element: local name: " + localName + " qname: " + qname + " uri: "+uri); int attrCount = attr.getLength();
if(attrCount>0) { System.out.println("Attributes:"); for(int i = 0 ; i<attrCount ; i++) { System.out.println(" Name : " + attr.getQName(i)); System.out.println(" Type : " + attr.getType(i)); System.out.println(" Value: " + attr.getValue(i)); } } } public void endElement(String uri, String localName, String qname) { System.out.println("End element: local name: " + localName + " qname: " + qname + " uri: "+uri); }
public void characters(char[] ch, int start, int length) { System.out.println("Characters: " + new String(ch, start, length)); }
Edit your question please. It's pretty unreadable as comments.
Paul
Thanks Paul, i got where the problem was and its working fine now.I followed the link given by you to understand. Thanks a lot.
A: 

You can try JDOM library.

http://www.jdom.org/docs/faq.html#a0360

Torres