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, ∼2126🔥, 0💬
Popular Posts:
JDK 11 jdk.jfr.jmod is the JMOD file for JDK 11 JFR module. JDK 11 JFR module compiled class files a...
What Is jaxb-api-2.1.6.jar? Java Architecture for XML Binding (JAXB) is a Java API that allows Java ...
Apache Avalon began in 1999 as the Java Apache Server Framework and in late 2002 separated from the ...
How to download and install xml-commons External Source Package? The source package contains Java so...
JRE 5 sunjce_provider.jar is the JAR file for JRE 5 Sun JCE Provider, which provides implementations...