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:
SaxXmlWhitespace.java - ignorableWhitespace() Event Handler
How do I catch those whitespace text contents that are removed from the SAX parser?
✍: FYIcenter
When DTD is applied to the XML document, the SAX parser will quietly remove
whitespace text contents and not call the characters() event handler.
If you want to catch those whitespace text contents during the parsing process, you need add the ignorableWhitespace() event handler as shown in this example, SaxXmlWhitespace.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 java.io.*;
public class SaxXmlWhitespace extends DefaultHandler {
static String dot = "............................................................";
static int l = 0;
static String show = "noshow";
public static void main(String[] args) throws Exception {
SAXParserFactory f = SAXParserFactory.newInstance();
SAXParser p = f.newSAXParser();
System.out.println("Parser class: "+p.getClass().getName());
show = args[1];
p.parse(new File(args[0]), new SaxXmlWhitespace());
}
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 ignorableWhitespace(char[] ch, int start, int length) {
if (show.equals("show")) {
System.out.print("["+(new String(ch,start,length))+"]");
}
}
}
Compile and run the example program, SaxXmlWhitespace.java:
>\fyicenter\jdk-1.8.0\bin\javac SaxXmlWhitespace.java
>\fyicenter\jdk-1.8.0\bin\java SaxXmlWhitespace UserDTD.xml show
Parser class: com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl
.User[
]
..ID(101)[
]
..BirthDate(1970-01-01+00:01)[
]
..Name(Frank Y. Ivy)[
]
..Sex( Male)[
]
If you run it again with "noshow", you will not see those whitespace text contents in the output:
>\fyicenter\jdk-1.8.0\bin\java SaxXmlWhitespace UserDTD.xml noshow Parser class: com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl .User ..ID(101) ..BirthDate(1970-01-01+00:01) ..Name(Frank Y. Ivy) ..Sex( Male)
⇒ SaxXmlDtdValidation.java - SAX Parser with DTD Validation
⇐ SAX Parser for XML File with DTD
2017-12-09, ∼2332🔥, 0💬
Popular Posts:
MP3SPI is a Java Service Provider Interface that adds MP3 (MPEG 1/2/2.5 Layer 1/2/3) audio format su...
ANTLR is a powerful parser generator for multiple programming languages including Java. ANTLR contai...
How to perform XML Schema validation with dom\Writer.java provided in the Apache Xerces package? You...
JDK 11 jdk.crypto.mscapi.jmod is the JMOD file for JDK 11 Crypto MSCAPI module. JDK 11 Crypto MSCAPI...
How to run "jarsigner" command from JDK tools.jar file? "jarsigner" command allows you to digitally ...