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:
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, 1611🔥, 0💬
Popular Posts:
What JAR files are required to run dom\Counter.java provided in the Apache Xerces package? You can f...
JDK 11 jdk.internal.JVM Stat.jmod is the JMOD file for JDK 11 Internal Jvmstat module. JDK 11 Intern...
What Is jms.jar? I heard it's related to JMS (Java Message Service) 1.1? The if you have an jms.jar ...
JDK 11 jdk.jdeps.jmod is the JMOD file for JDK 11 JDeps tool, which can be invoked by the "jdeps" co...
The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms, it was develo...