IntelliJ Java IDE
The moose likes Web Services Certification (SCDJWS/OCPJWSD) and the fly likes Problems Validating XML against Schema Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Web Services Certification (SCDJWS/OCPJWSD)
Reply 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: 51

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, Core Spring 3.0 Certified Professional, OCE Java EE 6 Web Services Developer(In Progress)
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2159
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: 51

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: 51

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
 
 
subject: Problems Validating XML against Schema
 
Threads others viewed
Error loading the report template: org.xml.sax.SAXParseException: cvc-complex-type.3.2.2: Attribute
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
InvalidMappingException in Hibernate
where to put the XML file for dependency injection.
IntelliJ Java IDE

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture