Skip to content

Instantly share code, notes, and snippets.

@9SQ 9SQ/SaxHandler.java
Created Aug 18, 2014

Embed
What would you like to do?
PuSHで来た気象庁XMLをSAXでパース
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.xml.bind.DatatypeConverter;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
public class SaxHandler extends DefaultHandler {
boolean entryFlag; // <entry></entry>内であることを記憶するフラグ
String nowElements; // 現在の要素名を記憶する
public void startDocument() {
}
public void startElement(String uri, String localName, String qName, Attributes attributes) {
nowElements = qName;
if (nowElements.equals("entry")) {
entryFlag = true;
}
if (entryFlag) {
int length = attributes.getLength();
if (length != 0) {
for (int i = 0; i < length; i++) {
if (attributes.getQName(i).equals("href")) {
System.out.println("link: " + attributes.getValue(i));
}
}
}
}
}
public void characters(char[] ch, int start, int length) {
String str = new String(ch, start, length);
if (entryFlag) {
if (nowElements.equals("title")) {
System.out.println("title: " + str);
}
if (nowElements.equals("id")) {
System.out.println("id: " + str.substring(9));
}
if (nowElements.equals("updated")) {
Calendar dateTime = DatatypeConverter.parseDateTime(str);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
System.out.println("updated: " + dateFormat.format(dateTime.getTime()));
}
if (nowElements.equals("name")) {
System.out.println("author.name: " + str);
}
}
}
public void endElement(String uri, String localName, String qName) {
if (qName.equals("entry")) {
entryFlag = false;
}
nowElements = "";
}
public void endDocument() {
}
}
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
@WebServlet("/subscriber")
public class Subscriber extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String verifyToken = "this_is_test"; // 届け出たトークン
public Subscriber() {
super();
}
// 購読意思確認時の処理
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String hubMode = request.getParameter("hub.mode");
String hubChallenge = request.getParameter("hub.challenge");
String hubVerifyToken = request.getParameter("hub.verify_token");
if (hubMode != null && !hubMode.isEmpty() && hubChallenge != null && !hubChallenge.isEmpty() && hubVerifyToken != null && !hubVerifyToken.isEmpty()) {
if (hubVerifyToken.equals(verifyToken)) {
if (hubMode.equals("subscribe") || hubMode.equals("unsubscribe")) {
response.setContentType("text/plain;charset=UTF8");
response.setStatus(HttpServletResponse.SC_OK);
PrintWriter out = response.getWriter();
out.print(hubChallenge);
}
} else {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Bad Request (verify token mismatch)");
}
} else {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Bad Request (parameter not set)");
}
}
// フィード受信時の処理
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Message Bodyからfeedを取得
InputStream jmafeed = request.getInputStream();
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
SAXParser parser;
try {
parser = saxParserFactory.newSAXParser();
parser.parse(jmafeed, new SaxHandler());
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.