Java Reference
In-Depth Information
DocumentBuilder builder = factory.newDocumentBuilder();
Document document =
builder.parse(new InputSource(new StringReader(xmlText)));
return newStocksFromXMLDocument(document);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// If we end up here, something went wrong:
return null;
}
public static Set<Stock> newStocksFromXMLDocument(Document document) {
Set<Stock> stocksSet = new HashSet<Stock>();
NodeList stockElements = document.getElementsByTagName("Stock");
for (int i = 0; i < stockElements.getLength(); i++) {
Stock stock = newStockFromXMLElement(
(Element) stockElements.item(i));
stocksSet.add(stock);
}
return stocksSet;
}
public static Stock newStockFromXMLElement(Element node) {
String symbol, name;
double last, open, high, low, marketCap,
previousClose, change, percentageChange, annRangeLow,
annRangeHigh, earns, pe;
long volume;
Date timestamp;
symbol = getEl(node, "Symbol");
name = getEl(node, "Name");
last = Double.parseDouble(getEl(node, "Last"));
open = Double.parseDouble(getEl(node, "Open"));
high = Double.parseDouble(getEl(node, "High"));
low = Double.parseDouble(getEl(node, "Low"));
volume = Long.parseLong(getEl(node, "Volume"));
marketCap = Double.parseDouble(getEl(node, "MktCap").replace("B", ""));
previousClose = Double.parseDouble(getEl(node, "PreviousClose"));
percentageChange = Double.parseDouble(getEl(node, "PercentageChange")
.replace("%", "")
.replace("+", "")
.replace("-", ""));
change = Double.parseDouble(getEl(node, "Change")
.replace("+", "")
.replace("-", ""));
String[] annRange = getEl(node, "AnnRange").split(" - ");
annRangeLow = Double.parseDouble(annRange[0]);
annRangeHigh = Double.parseDouble(annRange[1]);
earns = Double.parseDouble(getEl(node, "Earns"));
pe = Double.parseDouble(getEl(node, "P-E"));
Search WWH ::




Custom Search