aspose file tools*
The moose likes XML and Related Technologies and the fly likes validation question od XML schemas Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "validation question od XML schemas" Watch "validation question od XML schemas" New topic
Author

validation question od XML schemas

mou haj
Ranch Hand

Joined: Sep 12, 2001
Posts: 81
Hi
noe im trying to validate my XML with .xsd
and getting error
Parsing error: error The content of element type "memories" must match "(memory)".
XML
---
<?xml version="1.0"?>
<memories xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://localhost/estart/memory.xsd">
<memory tapeid="23412">
<subdate>2002-01-01</subdate>
<donor>John Baker</donor>
<subject>Fishing off Pier 60</subject>
</memory>
<memory tapeid="23692">
<subdate>2002-01-01</subdate>
<donor>Elizabeth Davison</donor>
<subject>Beach volleyball</subject>
</memory>
</memories>
XSD
*****

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="memories">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="memory" type="memoryType"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="memoryType">
<xsd:sequence>
<xsd:element name="subdate" type="xsd ate"/>
<xsd:element name="donor" type="xsd:string"/>
<xsd:element name="subject" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="tapeid" type="xsd:integer" />
</xsd:complexType>
</xsd:schema>

THe java class
==============

public class TestXSD {

public static void main (String args[]) {
File docFile = new File("C:/XML/memory.xml");

try {
DOMParser parser = new DOMParser();
parser.setFeature("http://xml.org/sax/features/validation", true);

parser.setProperty(
"http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
"http://localhost/estart/memory.xsd");

ErrorChecker errors = new ErrorChecker();
parser.setErrorHandler(errors);
parser.parse("C:/XML/memory.xml");

} catch (Exception e) {
System.out.print("Problem parsing the file.");
}
}

/**
* TestXSD constructor comment.
*/
public TestXSD() {
super();
}

}
and the ErrorChecker method
public class ErrorChecker extends DefaultHandler
{
public ErrorChecker() {
}
public void error (SAXParseException e) {
System.out.println("Parsing error: error "+e.getMessage());
}
public void warning (SAXParseException e) {
System.out.println("Parsing problem: warning "+e.getMessage());
}
public void fatalError (SAXParseException e) {
System.out.println("Parsing error: fatalError "+e.getMessage());
System.out.println("Cannot continue.");
System.exit(1);
}


}

PLEASE HELP . Im clueless :-(
mou haj
Ranch Hand

Joined: Sep 12, 2001
Posts: 81
i found out )
I shud have written
<xs:element name="memory" type="memoryType" minOccurs="0" maxOccurs="unbounded"/>
thanx
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
minOccurs="0"
yup, if this is not mentioned, it is treated as a required element. I ran into the same thing the other day.
- madhav


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
mou haj
Ranch Hand

Joined: Sep 12, 2001
Posts: 81
Hi Madhav,
Im very new to XML...
Can you tell me suppose I have an XML file and I have the XML schema .. how now ill have to extract the data and fill in a database. I mean I know ill have to parsing... but how.. can u gimme some sample code...
thanx
Moumita
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: validation question od XML schemas
 
Similar Threads
Error in parsing XML
Having trouble getting xml to validate
Issue in validating vanila XML
problem with validating sax parser
How to use DomParser.parse() to parse an XML String instead of the XML location?