Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

validation question od XML schemas

 
mou haj
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i found out )
I shud have written
<xs:element name="memory" type="memoryType" minOccurs="0" maxOccurs="unbounded"/>
thanx
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
mou haj
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic