Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (101)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (309)
Collections:
Other Resources:
SaxXmlXsValidation.java - SAX Parser with XS Validation
How to parse an XML file with XS (XML Schema) validation using the SAX API?
✍: FYIcenter
if you want to parse an XML file with XS (XML Schema) validation using the SAX API, you can follow these suggestions:
1. Set the validation flag to true on SAXParserFactory object:
SAXParserFactory f = SAXParserFactory.newInstance(); f.setValidating(Boolean.parseBoolean(args[1]));
2. Turn Schema Validation on and Namespace Awareness on:
f.setNamespaceAware(true); f.setFeature("http://apache.org/xml/features/validation/schema",true);
3. Provide the XML file with XML Schema location (User.xsd) provided, UserXsdError.xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <!-- Copyright (c) 2017 FYIcenter.com --> <fyi:User xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:fyi="http://fyicenter.com" xsi:schemaLocation="http://fyicenter.com User.xsd"> <fyi:ID>ONE</fyi:ID> <fyi:Name>Frank Y. Ivy</fyi:Name> <fyi:BirthDate>1970-01-01+00:01</fyi:BirthDate> <fyi:Sex> Male</fyi:Sex> </fyi:User>
Here is a complete example of parsing an XML with DTD validation using SAX API, SaxXmlXsValidation.java:
// Copyright (c) 2017 FYIcenter.com import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParser; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import java.io.*; public class SaxXmlXsValidation extends DefaultHandler { static String dot = "............................................................"; static int l = 0; public static void main(String[] args) throws Exception { SAXParserFactory f = SAXParserFactory.newInstance(); f.setNamespaceAware(true); f.setValidating(Boolean.parseBoolean(args[1])); f.setFeature("http://apache.org/xml/features/validation/schema",true); SAXParser p = f.newSAXParser(); System.out.println("Parser class: "+p.getClass().getName()); p.parse(new File(args[0]), new SaxXmlXsValidation()); } public void startElement(String uri, String lName, String qName, Attributes atts) { l++; System.out.print("\n"+dot.substring(0,l)+lName+qName); } public void endElement(String uri, String lName, String qName) { l--; } public void characters(char[] ch, int start, int length) { System.out.print("("+(new String(ch,start,length))+")"); } public void error(SAXParseException e) throws SAXException { System.out.println("\nError: "+e.toString()); } }
Compile and run the example program, SaxXmlXsValidation.java:
>\fyicenter\jdk-1.8.0\bin\javac SaxXmlXsValidation.java >\fyicenter\jdk-1.8.0\bin\java SaxXmlXsValidation UserXsdError.xml true Parser class: com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl .Userfyi:User ..IDfyi:ID(ONE) Error: org.xml.sax.SAXParseException; systemId: file:UserXsdError.xml; lineNumber: 6; columnNumber: 25; cvc-datatype-valid.1.2.1: 'ONE' is not a valid value for 'integer'. Error: org.xml.sax.SAXParseException; systemId: file:UserXsdError.xml; lineNumber: 6; columnNumber: 25; cvc-type.3.1.3: The value 'ONE' of element 'fyi:ID' is not valid. ..Namefyi:Name(Frank Y. Ivy) ..BirthDatefyi:BirthDate(1970-01-01+00:01) Error: org.xml.sax.SAXParseException; systemId: file:UserXsdError.xml; lineNumber: 8; columnNumber: 52; cvc-datatype-valid.1.2.1: '1970-01-01+00:01' is not a valid value for 'dateTime'. Error: org.xml.sax.SAXParseException; systemId: file:UserXsdError.xml; lineNumber: 8; columnNumber: 52; cvc-type.3.1.3: The value '1970-01-01+00:01' of element 'fyi:BirthDate' is not valid. ..Sexfyi:Sex(Male)
If you run it again with "false", you will not see any validation errors:
>\fyicenter\jdk-1.8.0\bin\java SaxXmlXsValidation UserXsdError.xml false Parser class: com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl .Userfyi:User ..IDfyi:ID(ONE) ..Namefyi:Name(Frank Y. Ivy) ..BirthDatefyi:BirthDate(1970-01-01+00:01) ..Sexfyi:Sex(Male)
The XML Schema file used in the above test is, User.xsd:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <!-- Copyright (c) 2017 FYIcenter.com --> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:fyi="http://fyicenter.com" targetNamespace="http://fyicenter.com" elementFormDefault="qualified"> <xsd:element name="User"> <xsd:complexType> <xsd:sequence> <xsd:element name="ID" type="xsd:integer"/> <xsd:element name="Name" type="xsd:string"/> <xsd:element name="BirthDate" type="xsd:dateTime"/> <xsd:element name="Sex" type="fyi:sexType"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:simpleType name="sexType"> <xsd:restriction base="xsd:NMTOKEN"> <xsd:enumeration value="Male"/> <xsd:enumeration value="Femal"/> </xsd:restriction> </xsd:simpleType> </xsd:schema>
⇒ Using Apache Xerces DOM Sample Programs
⇐ SaxXmlDtdValidation.java - SAX Parser with DTD Validation
2017-12-04, 1695🔥, 0💬
Popular Posts:
JDK 11 jdk.localedata.jmod is the JMOD file for JDK 11 Localedata module. JDK 11 Locale Data module ...
What is the sax\Writer.java provided in the Apache Xerces package? I have Apache Xerces 2.11.0 insta...
Apache Commons Lang 3 is the 3rd version of Apache Commons Lang, which provides a host of helper uti...
How to download and install Apache ZooKeeper Source Package? Apache ZooKeeper is an open-source serv...
maven-model-builder-3.8. 6.jaris the JAR file for Apache Maven 3.8.6 Model Builder module. Apache Ma...