Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (102)
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 (322)
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, ∼2296🔥, 0💬
Popular Posts:
JDK 11 jdk.javadoc.jmod is the JMOD file for JDK 11 Java Document tool, which can be invoked by the ...
iText is an ideal library for developers looking to enhance web- and other applications with dynamic...
kernel.jar is a component in iText Java library to provide low-level functionalities. iText Java lib...
What Is poi-examples-5.2.3.jar? poi-examples-5.2.3.jar is one of the JAR files for Apache POI 5.2.3,...
JRE 8 rt.jar is the JAR file for JRE 8 RT (Runtime) libraries. JRE (Java Runtime) 8 is the runtime e...