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:
SAX Parser for XML File with DTD
How to add DTD in XML file to remove whitespace text content during the SAX parsing process?
✍: FYIcenter
Sometimes when you are using the SAX parser to process an XML
file in a print pretty format, the characters() handler method
will be called for those extra whitespace text contents.
For example, look at the following print pretty XML file, User.xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- Copyright (c) 2017 FYIcenter.com -->
<User>
<ID>101</ID>
<BirthDate>1970-01-01+00:01</BirthDate>
<Name>Frank Y. Ivy</Name>
<Sex> Male</Sex>
</User>
If you run the example program, SaxXmlParser.java, presented in the previous tutorial with User.xml, you will see that characters() got called with those extra whitespaces:
>\fyicenter\jdk-1.8.0\bin\java SaxXmlParser User.xml
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)(
)
One way to fix the problem is to add the DTD section in XML file to provide the XML file structure to help the SAX parser:
<?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>
<BirthDate>1970-01-01+00:01</BirthDate>
<Name>Frank Y. Ivy</Name>
<Sex> Male</Sex>
</User>
If you run the example program, SaxXmlParser.java, presented in the previous tutorial with UserDTD.xml, you will not see any whitespaces text content:
>\fyicenter\jdk-1.8.0\bin\java SaxXmlParser UserDTD.xml 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)
⇒ SaxXmlWhitespace.java - ignorableWhitespace() Event Handler
⇐ SaxXmlParser.java - SAX XML Parser Example
2017-12-09, ∼1518🔥, 0💬
Popular Posts:
What is jxl.jar 2.6.12? jxl.jar 2.6.12 is the JAR file for Java Excel API 2.6.12, which is a Java li...
JDK 17 jdk.jdeps.jmod is the JMOD file for JDK 17 JDeps tool, which can be invoked by the "jdeps" co...
How to download and install ojdbc11.jar for Oracle 21c? ojdbc11.jar for Oracle 21c is a Java JDBC Dr...
maven-settings-builder-3 .8.6.jaris the JAR file for Apache Maven 3.8.6 Settings Builder module. Apa...
HttpComponents Client Source Code Files are provided in the source package file, httpcomponents-clie...