You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
138 lines
4.1 KiB
138 lines
4.1 KiB
package ru.defend.defdevteam.tstu;
|
|
|
|
import org.w3c.dom.Document;
|
|
import org.w3c.dom.Node;
|
|
import org.xml.sax.Attributes;
|
|
import org.xml.sax.InputSource;
|
|
import org.xml.sax.SAXException;
|
|
import org.xml.sax.XMLReader;
|
|
import org.xml.sax.helpers.DefaultHandler;
|
|
|
|
import java.io.StringReader;
|
|
import java.util.ArrayList;
|
|
import java.util.Collection;
|
|
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
import javax.xml.parsers.SAXParser;
|
|
import javax.xml.parsers.SAXParserFactory;
|
|
|
|
/**
|
|
* Created by thedefend on 29.11.16.
|
|
*/
|
|
|
|
public class FeedParser {
|
|
|
|
ParserCallback callback;
|
|
String inputXML;
|
|
|
|
interface ParserCallback {
|
|
void onSuccessParsing();
|
|
void onFailureParsing();
|
|
void contentDispatcher(String title, String text, String link);
|
|
}
|
|
|
|
void registerCallback(ParserCallback callback) {
|
|
this.callback = callback;
|
|
}
|
|
|
|
FeedParser(String inputXML) {
|
|
this.inputXML = inputXML;
|
|
}
|
|
|
|
// static NewsItemList.Group parsedItem;
|
|
// static void getParsedItem(String title, String description, String link, String date) {
|
|
// parsedItem = new NewsItemList().new Group(title, description, date);
|
|
// this.callback.contentDispatcher();
|
|
// }
|
|
|
|
void parseXML() {
|
|
try {
|
|
SAXParserFactory factory = SAXParserFactory.newInstance();
|
|
SAXParser parser = factory.newSAXParser();
|
|
ExtendedHandler handler = new ExtendedHandler();
|
|
InputSource inputSource = new InputSource(new StringReader(inputXML));
|
|
parser.parse(inputSource, handler);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
}
|
|
|
|
public class ExtendedHandler extends DefaultHandler {
|
|
boolean item = false;
|
|
boolean title = false;
|
|
boolean description = false;
|
|
boolean link = false;
|
|
boolean date = false;
|
|
String titleStr;
|
|
String descriptionStr;
|
|
String linkStr;
|
|
String dateStr;
|
|
|
|
@Override
|
|
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
|
|
if (qName.equalsIgnoreCase("item")) {
|
|
item = true;
|
|
}
|
|
if (item) {
|
|
String tagName = qName.toLowerCase();
|
|
switch (tagName) {
|
|
case "title": title = true;
|
|
break;
|
|
case "description": description = true;
|
|
break;
|
|
case "link": link = true;
|
|
break;
|
|
case "date": date = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void characters(char ch[], int start, int length) throws SAXException {
|
|
if(item) {
|
|
if (title) {
|
|
titleStr = new String(ch, start, length);
|
|
title = false;
|
|
} else if (description) {
|
|
descriptionStr = new String(ch, start, length);
|
|
description = false;
|
|
} else if (link) {
|
|
linkStr = new String(ch, start, length);
|
|
link = false;
|
|
} else if (date) {
|
|
dateStr = new String(ch, start, length);
|
|
date = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void endElement(String uri, String localName, String qName) throws SAXException {
|
|
if (title) {
|
|
title = false;
|
|
return;
|
|
} else if (description) {
|
|
description = false;
|
|
return;
|
|
} else if (link) {
|
|
link = false;
|
|
return;
|
|
} else if (date) {
|
|
date = false;
|
|
return;
|
|
}
|
|
if (item) {
|
|
item = false;
|
|
callback.contentDispatcher(titleStr, descriptionStr, "");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void endDocument() throws SAXException {
|
|
callback.onSuccessParsing();
|
|
}
|
|
}
|
|
|
|
}
|
|
|