OK, I manged to get a SAX parser working in a servlet. However, there are certain elements in the XML file that I am parsing that I want stored in an array, so that I call them up respectively when I write them out to the screen. For example, in the XML file that I'm parsing, there's a day tag that has three attributes: the day number, the day of the week, and the day of the month. When I parsed those three attributes, I only got every attribute in the last day tag of the XML file in their respective Strings. The same thing also applies when I try to parse the contents the hi and low tags that are also inside the day tag: I only get the last hi and low tags in the file. Is there any way to parse an attribute in consecutive order from each instance of a particular tag (i.e.: the attributes in the first day tag of the file go in the first element of seperate string arrays, then the second day tag's attributes go in the second element of the array, etc...) as well as the contents of multiple instance of a particular tags in an array (i.e.: the contents of the first hi tag go the first element of the array, then the contents of the sceond one in the second element, etc...) and store them in an array?
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
/**
* Simple Hello World Servlet
*
*/
public final class WeatherFleaServlet extends HttpServlet implements ContentHandler {
boolean inWeatherElement;
boolean inLocElement;
boolean inTmpElement;
boolean inDayElement;
boolean inHiElement;
boolean inLowElement;
String loc=null;
String tmp=null;
String hi=null;
String low=null;
String day=null;
String dayNumber=null;
String dayName=null;
String actualDate=null;
String dayWeather=null;
/**
* Respond to a GET request for the content produced by
* this servlet.
*
*/
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
String areaCode = request.getParameter("areacode");
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.println("<html>");
writer.println("<head>");
writer.println("<title>Sample WeatherFlea Servlet Page</title>");
writer.println("</head>");
writer.println("<body bgcolor=white>");
//XMLReader parser = null;
try
{
XMLReader parser=XMLReaderFactory.createXMLReader();
//URL theFeed = new URL("http://xoap.weather.com/weather/local/"+areaCode+"?cc=*&dayf=5∏=xoap&link=xoap&cc&par=1006552051&key=a866e303730ffb33");
//BufferedReader in = new BufferedReader(new InputStreamReader(theFeed.openStream()));
parser.setContentHandler(this);
parser.parse("http://xoap.weather.com/weather/local/"+areaCode+"?cc=*&dayf=5∏=xoap&link=xoap&cc&par=1006552051&key=a866e303730ffb33");
writer.println("Location: "+loc+"<br>");//"Today, for "+loc+", the weather is "+tmp+" degress."
writer.println("Temperature: "+tmp+"<br>");
writer.println("Day Number "+dayNumber+"<br>");
writer.println("Day Name: "+dayName+"<br>");
writer.println("Actual Date: "+actualDate+"<br>");
writer.println("High: "+hi+"<br>");
writer.println("Low: "+low+"<br>");
}
catch(SAXException e)
{
//System.out.println(args[0]+" is not well formed");
System.out.println(e.getMessage());
}
catch(IOException e)
{
System.out.println("Due to an IOException, the parser could not check "+e.getMessage());//I don't know what to make of this as well.
}
writer.println("</body>");
writer.println("</html>");
}
public void startElement(String namespaceURI, String localName, String qualifiedName, Attributes atts) throws SAXException {
if(localName.equals("weather"))
{
inWeatherElement=true;
}
if(localName.equals("dnam"))
{
loc="";
inLocElement=true;
}
if(localName.equals("tmp"))
{
tmp="";
inTmpElement=true;
}
if(localName.equals("day"))
{
dayNumber=atts.getValue("d");
dayName=atts.getValue("t");
actualDate=atts.getValue("dt");
//inDayElement=true;
}
if(localName.equals("hi"));
{
hi="";
inHiElement=true;
}
if(localName.equals("low"));
{
low="";
inLowElement=true;
}
}
public void endElement(String namespaceURI, String localName, String qualifiedName) throws SAXException {
if(localName.equals("dnam"))
{
//loc = getText();
//System.out.println(loc);
inLocElement=false;
}
if(localName.equals("tmp"))
{
//tmp = getText();
//System.out.println(tmp);
inTmpElement=false;
}
if(localName.equals("hi"))
{
hi=getText();
inHiElement=false;
}
if(localName.equals("low"))
{
low=getText();
inLowElement=false;
}
if(localName.equals("day"))
{
//dayWeather="Today, for "+dayName+" "+actualDate+" "+loc+", the weather is "+tmp+" degress, with a high of "+hi+" degrees and a low of "+low+" degrees.";
//day = getText();
//System.out.println(day);
//inDayElement=false;
}
if(localName.equals("weather"))
{
inWeatherElement=false;
}
}
/**
* @return
*/
private String getText() {
// TODO Auto-generated method stub
return null;
}
public void characters(char[] text, int start, int length) throws SAXException {
if(inLocElement)
{
loc+=String.valueOf(text,start,length);
}
if(inTmpElement)
{
tmp+=String.valueOf(text,start,length);
}
if(inDayElement)
{
dayNumber+=String.valueOf(text,start,length);
dayName+=String.valueOf(text, start, length);
actualDate+=String.valueOf(text, start, length);
}
if(inHiElement)
{
hi+=String.valueOf(text, start, length);
}
if(inLowElement)
{
low+=String.valueOf(text, start, length);
}
}
public void setDocumentLocator(Locator locator) {}
public void startDocument() throws SAXException {
inLocElement=false;
inTmpElement=false;
//inDayElement=false;
}
public void endDocument() throws SAXException {}
public void startPrefixMapping(String prefix, String URI) throws SAXException {}
public void endPrefixMapping(String prefix) throws SAXException {}
public void ignorableWhitespace(char[] text, int start,int length) throws SAXException {}
public void processingInstruction(String target, String data) throws SAXException {}
public void skippedEntity(String name) throws SAXException {}
}
and the XML file that I'm parsing:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--This document is intended only for use by authorized licensees of The Weather Channel. Unauthorized use is prohibited. Copyright 1995-2004, The Weather Channel Enterprises, Inc. All Rights Reserved.-->
<weather ver="2.0">
<head>
<locale>en_US</locale>
<form>MEDIUM</form>
<ut>F</ut>
<ud>mi</ud>
<us>mph</us>
<up>in</up>
<ur>in</ur>
</head>
<loc id="10473">
<dnam>Bronx, NY (10473)</dnam>
<tm>7:04 PM</tm>
<lat>40.82</lat>
<lon>-73.86</lon>
<sunr>7:11 AM</sunr>
<suns>4:28 PM</suns>
<zone>-5</zone>
</loc>
<lnks type="prmo">
<link pos="1">
<l>http://www.weather.com/outlook/health/allergies/10473?par=xoap</l>
<t>Pollen Reports</t>
</link>
<link pos="2">
<l>http://www.weather.com/outlook/travel/flights/citywx/10473?par=xoap</l>
<t>Airport Delays</t>
</link>
<link pos="3">
<l>http://www.weather.com/outlook/events/special/result/10473?when=thisweek&par=xoap</l>
<t>Special Events</t>
</link>
<link pos="4">
<l>http://www.weather.com/services/desktop.html?par=xoap</l>
<t>Download Desktop Weather</t>
</link>
</lnks>
<cc>
<lsup>12/12/04 6:51 PM EST</lsup>
<obst>La Guardia Arpt., NY</obst>
<tmp>44</tmp>
<flik>38</flik>
<t>Mostly Cloudy</t>
<icon>27</icon>
<bar>
<r>29.69</r>
<d>rising</d>
</bar>
<wind>
<s>13</s>
<gust>N/A</gust>
<d>240</d>
<t>WSW</t>
</wind>
<hmid>53</hmid>
<vis>10.0</vis>
<uv>
<i>0</i>
<t>Low</t>
</uv>
<dewp>28</dewp>
<moon>
<icon>1</icon>
<t>Waxing Crescent</t>
</moon>
</cc>
<dayf>
<lsup>12/12/04 6:37 PM EST</lsup>
<day d="0" t="Sunday" dt="Dec 12">
<hi>N/A</hi>
<low>40</low>
<sunr>7:11 AM</sunr>
<suns>4:28 PM</suns>
<part p="d">
<icon>44</icon>
<t>N/A</t>
<wind>
<s>N/A</s>
<gust>N/A</gust>
<d>N/A</d>
<t>N/A</t>
</wind>
<ppcp>60</ppcp>
<hmid>N/A</hmid>
</part>
<part p="n">
<icon>11</icon>
<t>Showers</t>
<wind>
<s>16</s>
<gust>N/A</gust>
<d>231</d>
<t>SW</t>
</wind>
<ppcp>60</ppcp>
<hmid>69</hmid>
</part>
</day>
<day d="1" t="Monday" dt="Dec 13">
<hi>47</hi>
<low>33</low>
<sunr>7:12 AM</sunr>
<suns>4:28 PM</suns>
<part p="d">
<icon>30</icon>
<t>Partly Cloudy</t>
<wind>
<s>21</s>
<gust>N/A</gust>
<d>264</d>
<t>W</t>
</wind>
<ppcp>20</ppcp>
<hmid>56</hmid>
</part>
<part p="n">
<icon>29</icon>
<t>Partly Cloudy</t>
<wind>
<s>20</s>
<gust>N/A</gust>
<d>280</d>
<t>W</t>
</wind>
<ppcp>20</ppcp>
<hmid>54</hmid>
</part>
</day>
<day d="2" t="Tuesday" dt="Dec 14">
<hi>36</hi>
<low>24</low>
<sunr>7:12 AM</sunr>
<suns>4:28 PM</suns>
<part p="d">
<icon>34</icon>
<t>Mostly Sunny</t>
<wind>
<s>18</s>
<gust>N/A</gust>
<d>304</d>
<t>NW</t>
</wind>
<ppcp>20</ppcp>
<hmid>44</hmid>
</part>
<part p="n">
<icon>29</icon>
<t>Partly Cloudy</t>
<wind>
<s>14</s>
<gust>N/A</gust>
<d>321</d>
<t>NW</t>
</wind>
<ppcp>10</ppcp>
<hmid>44</hmid>
</part>
</day>
<day d="3" t="Wednesday" dt="Dec 15">
<hi>34</hi>
<low>29</low>
<sunr>7:13 AM</sunr>
<suns>4:28 PM</suns>
<part p="d">
<icon>32</icon>
<t>Sunny</t>
<wind>
<s>13</s>
<gust>N/A</gust>
<d>329</d>
<t>NNW</t>
</wind>
<ppcp>10</ppcp>
<hmid>40</hmid>
</part>
<part p="n">
<icon>31</icon>
<t>Clear</t>
<wind>
<s>10</s>
<gust>N/A</gust>
<d>309</d>
<t>NW</t>
</wind>
<ppcp>0</ppcp>
<hmid>47</hmid>
</part>
</day>
<day d="4" t="Thursday" dt="Dec 16">
<hi>44</hi>
<low>35</low>
<sunr>7:14 AM</sunr>
<suns>4:29 PM</suns>
<part p="d">
<icon>34</icon>
<t>Mostly Sunny</t>
<wind>
<s>19</s>
<gust>N/A</gust>
<d>274</d>
<t>W</t>
</wind>
<ppcp>10</ppcp>
<hmid>47</hmid>
</part>
<part p="n">
<icon>29</icon>
<t>Partly Cloudy</t>
<wind>
<s>13</s>
<gust>N/A</gust>
<d>276</d>
<t>W</t>
</wind>
<ppcp>20</ppcp>
<hmid>57</hmid>
</part>
</day>
</dayf>
</weather>