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:
SaxXmlParser.java - SAX XML Parser Example
How to parse an XML file with SAX (Simple API for XML) API?
✍: FYIcenter
If you want to parse an XML file with SAX (Simple API for XML),
you can these suggestions privded below:
1. Use the factory class to create SAXParser object:
SAXParserFactory f = SAXParserFactory.newInstance(); SAXParser p = f.newSAXParser();
2. Extend the DefaultHandler class to replace default event handlers with your logics:
3. Call the parse() method on the Parser object to parse an XML file with an object that has your own event handlers:
p.parse(new File(args[0]), new SaxXmlParser());
Here is a complete example of parsing an XML file with SAX API, SaxXmlParser.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 SaxXmlParser extends DefaultHandler { static String dot = "............................................................"; static int l = 0; public static void main(String[] args) throws Exception { SAXParserFactory f = SAXParserFactory.newInstance(); SAXParser p = f.newSAXParser(); System.out.println("Parser class: "+p.getClass().getName()); p.parse(new File(args[0]), new SaxXmlParser()); } 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))+")"); } }
Compile and run the example program, SaxXmlParser.java:
>\fyicenter\jdk-1.8.0\bin\javac SaxXmlParser.java >\fyicenter\jdk-1.8.0\bin\java SaxXmlParser UserCompact.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)
Here is the XML file, UserCompact.xml, used in the above example:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <User><ID>101</ID><BirthDate>1970-01-01+00:01</BirthDate><Name>Frank Y. Ivy</Name><Sex> Male</Sex></User>
⇒ SAX Parser for XML File with DTD
⇐ SaxClassInfo.java - SAX Implementation Class
2017-12-09, 1790🔥, 0💬
Popular Posts:
What is the dom\GetElementsByTagName .javaprovided in the Apache Xerces package? I have Apache Xerce...
Snappy-Java is a Java port of the "snappy", a fast C++ compresser/decompresser developed by Google. ...
What Is log4j-1.2.13.jar? I got the JAR file from logging-log4j-1.2.13.zip .log4j-1.2.13.jar is the ...
JDK 17 java.base.jmod is the JMOD file for JDK 17 Base module. JDK 17 Base module compiled class fil...
This package is the backport of java.util.concurrent API, introduced in Java 5.0 and further refined...