|
|
@ -2,12 +2,20 @@ 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. |
|
|
@ -21,6 +29,7 @@ public class FeedParser { |
|
|
|
interface ParserCallback { |
|
|
|
void onSuccessParsing(); |
|
|
|
void onFailureParsing(); |
|
|
|
void contentDispatcher(String title, String text, String link); |
|
|
|
} |
|
|
|
|
|
|
|
void registerCallback(ParserCallback callback) { |
|
|
@ -31,22 +40,99 @@ public class FeedParser { |
|
|
|
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 { |
|
|
|
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
|
|
|
// Создается дерево DOM документа из файла
|
|
|
|
Document document = documentBuilder.parse(inputXML); |
|
|
|
|
|
|
|
// Получаем корневой элемент
|
|
|
|
Node root = document.getDocumentElement(); |
|
|
|
String exp = root.getTextContent(); |
|
|
|
//group.set("Node",exp,"");
|
|
|
|
//newsItemList.add(group);
|
|
|
|
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(); |
|
|
|
} |
|
|
|
|
|
|
|
callback.onSuccessParsing(); |
|
|
|
} |
|
|
|
|
|
|
|
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(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|