mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
25038da2a1
#story[572]
55 lines
1.6 KiB
Java
55 lines
1.6 KiB
Java
package seng302.models.parsers;
|
|
|
|
import org.w3c.dom.Document;
|
|
import org.xml.sax.InputSource;
|
|
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.StringReader;
|
|
|
|
/**
|
|
* Created by Haoming Yin (hyi25) on 16/3/2017
|
|
*/
|
|
public abstract class FileParser {
|
|
|
|
private String filePath;
|
|
|
|
public FileParser() {}
|
|
|
|
public FileParser(String path) {
|
|
this.filePath = path;
|
|
}
|
|
|
|
protected Document parseFile() {
|
|
try {
|
|
InputStream is = getClass().getResourceAsStream(this.filePath);
|
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
DocumentBuilder builder = factory.newDocumentBuilder();
|
|
Document doc = builder.parse(is);
|
|
// optional, in order to recover info from broken line.
|
|
doc.getDocumentElement().normalize();
|
|
return doc;
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
protected Document parseFile(String xmlString) {
|
|
try {
|
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
DocumentBuilder builder = factory.newDocumentBuilder();
|
|
Document doc = builder.parse(new InputSource(new StringReader(xmlString)));
|
|
// optional, in order to recover info from broken line.
|
|
doc.getDocumentElement().normalize();
|
|
return doc;
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return null;
|
|
}
|
|
}
|