Hide
Siafoo is here to make coding less frustrating and to save you time. Join Siafoo Now or Learn More

SAX parser skeleton Atom Feed 0

In Brief A self contained skeleton SAX parser. You can modify this code to actually do something useful with an XML file.... more
# 's
  1import java.io.IOException;
2import java.io.InputStream;
3import java.net.MalformedURLException;
4import java.net.URL;
5
6import javax.xml.parsers.ParserConfigurationException;
7import javax.xml.parsers.SAXParserFactory;
8
9import org.xml.sax.Attributes;
10import org.xml.sax.ContentHandler;
11import org.xml.sax.InputSource;
12import org.xml.sax.Locator;
13import org.xml.sax.SAXException;
14import org.xml.sax.XMLReader;
15
16
17
18public class SaxSkeleton implements ContentHandler {
19
20 // This helps us find the location of parsing events
21 private Locator m_Locator;
22
23 @Override
24 public void startDocument() throws SAXException {
25 // Called when parsing begins on the document.
26 // This is where you can initialize the various structures
27 // you need for storing the rest of the information from the document
28 System.out.println("Document Start");
29 }
30
31 @Override
32 public void endDocument() throws SAXException {
33 // Called when parsing reaches the end of the document
34 System.out.println("Document End");
35 }
36
37 @Override
38 public void startElement(String uri, String localName, String name, Attributes atts) throws SAXException {
39 // Called when processing the element start tag
40 System.out.println("Element Start - " + formatNodeInfo(uri, localName, name));
41
42 for(int i = 0; i < atts.getLength(); ++i)
43 {
44 System.out.println("\t attribute: " + atts.getQName(i) + "=" + atts.getValue(i));
45 }
46 }
47
48 @Override
49 public void endElement(String uri, String localName, String name) throws SAXException {
50 // Called when parsing reaches the end of the element
51 System.out.println("Element End - " + formatNodeInfo(uri, localName, name));
52 }
53
54 @Override
55 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
56 // Called when ignorable white space is encountered by the parser
57 }
58
59 @Override
60 public void processingInstruction(String target, String data) throws SAXException {
61 System.out.println("Encountered process instruction: " + target);
62 }
63 @Override
64 public void skippedEntity(String name) throws SAXException {
65 System.out.println("Skipping element: " + name);
66 }
67
68 @Override
69 public void startPrefixMapping(String prefix, String uri) throws SAXException {
70 // It's doubtful that you need this
71 // See http://www.saxproject.org/apidoc/org/xml/sax/ContentHandler.html#startPrefixMapping(java.lang.String,%20java.lang.String)
72 System.out.println(String.format("Start of Prefix mapping for prefix: '%s' with uri '%s'", prefix, uri));
73 }
74
75 @Override
76 public void endPrefixMapping(String prefix) throws SAXException {
77 // It's doubtful that you need this
78 // See http://www.saxproject.org/apidoc/org/xml/sax/ContentHandler.html#endPrefixMapping(java.lang.String)
79 System.out.println("End prefix mapping for prefix: " + prefix);
80 }
81
82 @Override
83 public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
84 // Called when character data (CDATA) sections are encountered by the parser
85 }
86
87 @Override
88 public void setDocumentLocator(Locator locator) {
89 // The document locator helps your application find out where events are coming from
90 // only valid between StartDocument and EndDocument events
91 m_Locator = locator;
92 }
93
94 private String formatNodeInfo(String uri, String localName, String name)
95 {
96 // There's gotta be a cleaner way of doing this
97 String result = "";
98
99 if(name.length() > 0)
100 result += "Name:" + name + " ";
101 if(uri.length() > 0)
102 result += "URI:" + uri + " ";
103 if(localName.length() > 0)
104 result += "Local Name:" + localName + " ";
105
106 result += "( Line: " + this.m_Locator.getLineNumber() + " )";
107
108 return result;
109 }
110
111 private static InputStream getRemoteDocument(String url){
112 try {
113 URL urlObject = new URL(url);
114 urlObject.getContent();
115 return (InputStream)urlObject.getContent();
116 } catch (MalformedURLException e) {
117 System.out.println("Invalid URL" + url);
118 } catch (IOException e) {
119 e.printStackTrace();
120 }
121 System.exit(1);
122 return null;
123 }
124
125 /**
126 * @param args
127 */
128 public static void main(String[] args) {
129
130 if (args.length != 1){
131 System.out.println("Usage: java SaxSkeleton file|url");
132 return;
133 }
134
135 // Initialize the parser
136 SAXParserFactory spf = SAXParserFactory.newInstance();
137
138 XMLReader xmlReader;
139 try {
140 xmlReader = spf.newSAXParser().getXMLReader();
141 SaxSkeleton contentProvider = new SaxSkeleton();
142 xmlReader.setContentHandler(contentProvider);
143 InputSource inputSrc;
144
145 if(args[0].startsWith("http://"))
146 {
147 inputSrc = new InputSource(getRemoteDocument(args[0]));
148 }
149 else
150 {
151 inputSrc = new InputSource(args[0]);
152 }
153
154 xmlReader.parse(inputSrc);
155 } catch (SAXException e) {
156 e.printStackTrace();
157 } catch (ParserConfigurationException e) {
158 e.printStackTrace();
159 } catch (IOException e) {
160 e.printStackTrace();
161 }
162 }
163
164
165}

A self contained skeleton SAX parser. You can modify this code to actually do something useful with an XML file.

You would need a SAX parser library, like

You can use it like:

To parse a remote xml file: java SaxSkeleton http://www.siafoo.net/activity/feed

To parse a simple file: java SaxSkeleton some_file.xml

Check out The SAX Project for more information about what a SAX parser is and the interface it provides.