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, ∼2015🔥, 0💬
Popular Posts:
JDK 17 java.desktop.jmod is the JMOD file for JDK 17 Desktop module. JDK 17 Desktop module compiled ...
What Is commons-logging-1.2.jar? commons-logging-1.2.jar is the JAR file for Apache Commons Logging ...
commons-lang-2.6.jar is the JAR file for Apache Commons Lang 2.6, which provides a host of helper ut...
Jackson is "the Java JSON library" or "the best JSON parser for Java". Or simply as "JSON for Java"....
JDK 17 java.rmi.jmod is the JMOD file for JDK 17 RMI (Remote Method Invocation) module. JDK 17 RMI m...