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:
SaxXmlDtdValidation.java - SAX Parser with DTD Validation
How to parse an XML file with DTD validation using the SAX API?
✍: FYIcenter
if you want to parse an XML file with DTD 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])); SAXParser p = f.newSAXParser();
3. Impement validation event handler methods, warning(), Error(), fatalError():
public void error(SAXParseException e) throws SAXException { System.out.println("\nError: "+e.toString()); }
3. Provide the XML file with DTD included, UserError.xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <!-- Copyright (c) 2017 FYIcenter.com --> <!DOCTYPE User [ <!ELEMENT User (ID+, BirthDate+, Name+, Sex+)> <!ELEMENT ID (#PCDATA)> <!ELEMENT BirthDate (#PCDATA)> <!ELEMENT Name (#PCDATA)> <!ELEMENT Sex (#PCDATA)> ]> <User> <ID>101</ID> <Name>Frank Y. Ivy</Name> </User>
Here is a complete example of parsing an XML with DTD validation using SAX API, SaxXmlDtdValidation.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 SaxXmlDtdValidation extends DefaultHandler { static String dot = "............................................................"; static int l = 0; public static void main(String[] args) throws Exception { SAXParserFactory f = SAXParserFactory.newInstance(); f.setValidating(Boolean.parseBoolean(args[1])); SAXParser p = f.newSAXParser(); System.out.println("Parser class: "+p.getClass().getName()); p.parse(new File(args[0]), new SaxXmlDtdValidation()); } 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, SaxXmlDtdValidation.java:
>\fyicenter\jdk-1.8.0\bin\javac SaxXmlDtdValidation.java >\fyicenter\jdk-1.8.0\bin\java SaxXmlDtdValidation UserError.xml true Parser class: com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl .User ..ID(101) ..Name(Frank Y. Ivy) Error: org.xml.sax.SAXParseException; systemId: file:/fyicenter/UserError.xml; lineNumber: 14; columnNumber: 8; The content of element type "User" must match "(ID+,BirthDate+,Name+,Sex+)".
If you run it again with "false", you will not see any validation errors:
>\fyicenter\jdk-1.8.0\bin\java SaxXmlDtdValidation UserError.xml false Parser class: com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl .User ..ID(101) ..Name(Frank Y. Ivy)
⇒ SaxXmlXsValidation.java - SAX Parser with XS Validation
⇐ SaxXmlWhitespace.java - ignorableWhitespace() Event Handler
2017-12-09, 1917🔥, 0💬
Popular Posts:
JDK 11 jdk.javadoc.jmod is the JMOD file for JDK 11 Java Document tool, which can be invoked by the ...
How to read XML document with XML Schema validation from socket connections with the socket\DelayedI...
Jackson is "the Java JSON library" or "the best JSON parser for Java". Or simply as "JSON for Java"....
MP3SPI is a Java Service Provider Interface that adds MP3 (MPEG 1/2/2.5 Layer 1/2/3) audio format su...
This package is the backport of java.util.concurrent API, introduced in Java 5.0 and further refined...