Code:
<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="paket">
<xsd:annotation>
<xsd:documentation>
A sample element
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xsd:element name="devojke">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="devojka" maxOccurs="unbounded" minOccurs="0">
<xsd:complexType>
<xsd:attribute name="id"/>
<xsd:attribute name="sifoni"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="momci">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="momak" maxOccurs="unbounded" minOccurs="0">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="devojka" maxOccurs="unbounded">
<xsd:complexType>
<xsd:attribute name="refId"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="id"/>
<xsd:attribute name="ime"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="paket">
<xsd:annotation>
<xsd:documentation>
A sample element
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xsd:element name="devojke">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="devojka" maxOccurs="unbounded" minOccurs="0">
<xsd:complexType>
<xsd:attribute name="id"/>
<xsd:attribute name="sifoni"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="momci">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="momak" maxOccurs="unbounded" minOccurs="0">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="devojka" maxOccurs="unbounded">
<xsd:complexType>
<xsd:attribute name="refId"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="id"/>
<xsd:attribute name="ime"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
XML za validaciju:
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<paket>
<devojke>
<Xdevojka id="1" sifoni="4"/> <!-- NAMERNA GRESKA -->
<devojka id="2" sifoni="5"/>
<devojka id="3" sifoni="6"/>
</devojke>
<momci>
<momak id="1" ime="Goran">
<devojka refId="1"/>
<devojka refId="2"/>
</momak>
<momak id="2" ime="Mixailo">
<devojka refId="2"/>
<devojka refId="3"/>
</momak>
</momci>
</paket>
<?xml version="1.0" encoding="UTF-8" ?>
<paket>
<devojke>
<Xdevojka id="1" sifoni="4"/> <!-- NAMERNA GRESKA -->
<devojka id="2" sifoni="5"/>
<devojka id="3" sifoni="6"/>
</devojke>
<momci>
<momak id="1" ime="Goran">
<devojka refId="1"/>
<devojka refId="2"/>
</momak>
<momak id="2" ime="Mixailo">
<devojka refId="2"/>
<devojka refId="3"/>
</momak>
</momci>
</paket>
Java metoda:
Code:
private void validacijaXML() {
try {
// Parse an XML document into a DOM tree.
DocumentBuilder parser =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
org.w3c.dom.Document document = parser.parse(new File("test.xml"));
// Create a SchemaFactory capable of understanding WXS schemas.
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// Load a WXS schema, represented by a Schema instance.
Source schemaFile = new StreamSource(new File("../Shema/xmlSema.xsd"));
Schema schema = factory.newSchema(schemaFile);
// Create a Validator object, which can be used to validate
// an instance document.
Validator validator = schema.newValidator();
// Validate the DOM tree.
validator.validate(new DOMSource(document));
} catch (ParserConfigurationException e) {
// exception handling
System.out.println("Bla1!");
} catch (SAXException e) {
// exception handling - document not valid!
System.out.println("Nije validan xml!");
} catch (IOException e) {
// exception handling
System.out.println("Bla2!");
}
}
private void validacijaXML() {
try {
// Parse an XML document into a DOM tree.
DocumentBuilder parser =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
org.w3c.dom.Document document = parser.parse(new File("test.xml"));
// Create a SchemaFactory capable of understanding WXS schemas.
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// Load a WXS schema, represented by a Schema instance.
Source schemaFile = new StreamSource(new File("../Shema/xmlSema.xsd"));
Schema schema = factory.newSchema(schemaFile);
// Create a Validator object, which can be used to validate
// an instance document.
Validator validator = schema.newValidator();
// Validate the DOM tree.
validator.validate(new DOMSource(document));
} catch (ParserConfigurationException e) {
// exception handling
System.out.println("Bla1!");
} catch (SAXException e) {
// exception handling - document not valid!
System.out.println("Nije validan xml!");
} catch (IOException e) {
// exception handling
System.out.println("Bla2!");
}
}
I samo prodje i ne obradi gresku, kao da je sve ok!
Gde gresim?
V