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:
DomXmlDtdValidation.java - DOM Parser with DTD Validation
How to parse an XML file with DTD validation using the DOM API?
✍: FYIcenter
if you want to parse an XML file with DTD validation using the DOM API, you can follow these suggestions:
1. Set the validation flag to true on DocumentBuilderFactory object:
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance(); f.setValidating(Boolean.parseBoolean(args[1])); DocumentBuilder b = f.newDocumentBuilder();
2. 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 java.io.*; import javax.xml.parsers.*; import org.w3c.dom.*; public class DomXmlDtdValidation { static String dot = "............................................................"; public static void main(String[] args) throws Exception { DocumentBuilderFactory f = DocumentBuilderFactory.newInstance(); f.setValidating(Boolean.parseBoolean(args[1])); DocumentBuilder b = f.newDocumentBuilder(); Document d = b.parse(new File(args[0])); System.out.println("Implementation class:\n "+d.getClass().getName()); System.out.println("DOM object elements and text contents:"); Node n = d.getDocumentElement(); printText(n, 1); } public static void printText(Node n, int l) { String v = ""; if (n.getNodeType()==Node.TEXT_NODE) v = n.getTextContent(); System.out.println(dot.substring(0,l)+n.getNodeName()+":"+v); NodeList c = n.getChildNodes(); for (int i=0; i<c.getLength(); i++) { printText(c.item(i),l+1); } } }
Compile and run the example program, DomXmlSerializer.java:
>\fyicenter\jdk-1.8.0\bin\javac DomXmlDtdValidation.java >\fyicenter\jdk-1.8.0\bin\java DomXmlDtdValidation UserError.xml true Warning: validation was turned on but an org.xml.sax.ErrorHandler was not set, w hich is probably not what is desired. Parser will use a default ErrorHandler to print the first 0 errors. Please call the setErrorHandler method to fix this. Error: URI=file:UserError.xml Line=14: The content of element type "User" must match "(ID+,BirthDate+,Name+,Sex+)". Implementation class: com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl DOM object elements and text contents: .User: ..#text: ..ID: ...#text:101 ..#text: ..Name: ...#text:Frank Y. Ivy ..#text:
If you run it again with "false", you will not see any validation errors:
>\fyicenter\jdk-1.8.0\bin\java DomXmlDtdValidation UserError.xml false Implementation class: com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl DOM object elements and text contents: .User: ..#text: ..ID: ...#text:101 ..#text: ..Name: ...#text:Frank Y. Ivy ..#text:
⇒ DomXmlXsValidation.java - DOM Parser with XS Validation
⇐ DomXmlSerializer.java - Serialize DOM to XML String
2017-12-13, 2016🔥, 0💬
Popular Posts:
How to download and install mysql-connector-j-8.0.31 .zip?Connector/J Java library is a JDBC Driver ...
JDK 17 jdk.hotspot.agent.jmod is the JMOD file for JDK 17 Hotspot Agent module. JDK 17 Hotspot Agent...
JDK 17 jdk.jdi.jmod is the JMOD file for JDK 17 JDI (Java Debug Interface) tool. JDK 17 JDI tool com...
JDK 11 java.naming.jmod is the JMOD file for JDK 11 Naming module. JDK 11 Naming module compiled cla...
JDOM provides a solution for using XML from Java that is as simple as Java itself. There is no compe...