File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Services Certification (SCDJWS/OCEJWSD) and the fly likes Problems Validating XML against Schema Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Web Services Certification (SCDJWS/OCEJWSD)
Bookmark "Problems Validating XML against Schema" Watch "Problems Validating XML against Schema" New topic
Author

Problems Validating XML against Schema

Avinash Ga
Ranch Hand

Joined: Aug 13, 2011
Posts: 78

hi ranchers,


im getting exception while validating the xml against the schema.

xml file is like

persondetails.xml


<?xml version="1.0" encoding="UTF-8"?>
<person name="avinash" age="27" gender="male">
<address type="email">xgjjkk@gmail.com</address>
<address type="phone">khkhlll</address>
<address type="company">jgjhgjhgk</address>
</person>

persondetails.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/persondetails" xmlns:tns="http://www.example.org/persondetails" elementFormDefault="qualified">

<element name="person">
<complexType>
<sequence>
<element name="address">
<complexType>
<attribute name="type">
<simpleType>
<restriction base="string">
<enumeration value="email"></enumeration>
<enumeration value="phone"></enumeration>
<enumeration value="company"></enumeration>
</restriction>
</simpleType>
</attribute>
</complexType>
</element>
</sequence>
<attribute name="name" type="string"></attribute>
<attribute name="age" type="int"></attribute>
<attribute name="gender" type="string"></attribute>
</complexType>
</element>
</schema>


java code is :

package simplevalidator;

import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

public class SchemaValidator {

/**
* @param args
*/
public static void main(String[] args) throws Exception {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new StreamSource("src//persondetails.xsd"));
Validator validator = schema.newValidator();
validator.validate(new StreamSource("src//persondetails.xml"));
}

}


Exception is :

Exception in thread "main" org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'person'.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1916)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:705)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:400)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:626)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3104)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:922)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.jaxp.validation.StreamValidatorHelper.validate(StreamValidatorHelper.java:144)
at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validate(ValidatorImpl.java:111)
at javax.xml.validation.Validator.validate(Validator.java:127)
at simplevalidator.SchemaValidator.main(SchemaValidator.java:18)



can you help me fixing this guys? im a newbie to XSD.




Avinash G.A
OCP Java SE 6 Programmer, OCP Java EE 5 Web Component Developer, OCE Java EE 6 Web Services Developer, VMware Certified Core Spring 3.x Developer, EMC Proven Professional (ISM-V2)
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
What is the difference between the following two attributes?

xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.example.org/persondetails"
Avinash Ga
Ranch Hand

Joined: Aug 13, 2011
Posts: 78

Jimmy Clark wrote:What is the difference between the following two attributes?

xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.example.org/persondetails"


Hi Jim,

As im a newbie in XSD i created it using eclipse. The one posted here is exact one generated by eclipse. One of the yahoo answers says that namespace is related to SOAP.


Thanks
Avinash Ga
Ranch Hand

Joined: Aug 13, 2011
Posts: 78

Avinash Ga wrote:
Jimmy Clark wrote:What is the difference between the following two attributes?

xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.example.org/persondetails"


Hi Jim,

As im a newbie in XSD i created it using eclipse. The one posted here is exact one generated by eclipse. One of the yahoo answers says that namespace is related to SOAP.


Thanks



Hi Jim,


the problem was the missing xmlns in the xml. im now getting somewhat desired results.


Thnaks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problems Validating XML against Schema
 
Similar Threads
InvalidMappingException in Hibernate
Error loading the report template: org.xml.sax.SAXParseException: cvc-complex-type.3.2.2: Attribute
where to put the XML file for dependency injection.
Attribute "value" must be declared for element type "property"???
cvc-complex-type.2.3:Element 'beans' cannot have character [children] type's content type is element