• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Problems Validating XML against Schema

 
Ranch Hand
Posts: 78
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



 
Ranch Hand
Posts: 2187
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 78
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Humans and their filthy friendship brings nothing but trouble. My only solace is this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic