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, 1825🔥, 0💬
Popular Posts:
How to download and install iText7-Core-7.1.4.zip? iText7-Core-7.1.4.zip is the binary package of iT...
What Is fop.jar? I got it from the fop-2.7-bin.zip. fop.jar in fop-2.7-bin.zip is the JAR file for F...
JAX-WS is an API for building web services and clients. It is the next generation Web Services API r...
Jetty provides an HTTP server, HTTP client, and javax.servlet container. These components are open s...
Rhino JavaScript Java Library is an open-source implementation of JavaScript written entirely in Jav...