/** * Outputs all of the nodes and events to console. This method is only really used for debugging. */ public void output() { for (Node node : this.nodes) { System.out.println(node.toString()); } for (Event event : this.events) { System.out.println(event.toString()); } }
public void write(File file) throws ParserConfigurationException, DOMException, UnsupportedEncodingException, TransformerFactoryConfigurationError, TransformerException { DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = docBuilder.newDocument(); Element elementRoot = document.createElement("Log"); document.appendChild(elementRoot); Element elementNodes = document.createElement("Nodes"); Element elementEvents = document.createElement("Events"); elementRoot.appendChild(elementNodes); elementRoot.appendChild(elementEvents); for (Node i : this.nodes) { // Node Element element = document.createElement("Node"); elementNodes.appendChild(element); // Id Element elementId = document.createElement("Id"); elementId.setTextContent(new String(i.getId(), "UTF-8")); element.appendChild(elementId); } for (Event i : this.events) { if (i instanceof EventNodeGps) { EventNodeGps eventNodeGps = (EventNodeGps) i; // GPS Update Element element = document.createElement("GpsUpdate"); elementEvents.appendChild(element); // Id Element elementId = document.createElement("Id"); elementId.setTextContent(new String(eventNodeGps.getNode().getId(), "UTF-8")); element.appendChild(elementId); // X Element elementX = document.createElement("X"); elementX.setTextContent(Double.toString(eventNodeGps.getCoordinates().getX())); element.appendChild(elementX); // Y Element elementY = document.createElement("Y"); elementY.setTextContent(Double.toString(eventNodeGps.getCoordinates().getY())); element.appendChild(elementY); // Timestamp Element elementTimestamp = document.createElement("Timestamp"); elementTimestamp.setTextContent(Long.toString(eventNodeGps.getTimestamp())); element.appendChild(elementTimestamp); } else if (i instanceof EventNodeMagnetometer) { EventNodeMagnetometer eventNodeMagnetometer = (EventNodeMagnetometer) i; // GPS Update Element element = document.createElement("MagnetometerUpdate"); elementEvents.appendChild(element); // Id Element elementId = document.createElement("Id"); elementId.setTextContent(new String(eventNodeMagnetometer.getNode().getId(), "UTF-8")); element.appendChild(elementId); // X Element elementX = document.createElement("X"); elementX.setTextContent(Double.toString(eventNodeMagnetometer.getX())); element.appendChild(elementX); // Y Element elementY = document.createElement("Y"); elementY.setTextContent(Double.toString(eventNodeMagnetometer.getY())); element.appendChild(elementY); // Y Element elementZ = document.createElement("Z"); elementZ.setTextContent(Double.toString(eventNodeMagnetometer.getZ())); element.appendChild(elementZ); // Timestamp Element elementTimestamp = document.createElement("Timestamp"); elementTimestamp.setTextContent(Long.toString(eventNodeMagnetometer.getTimestamp())); element.appendChild(elementTimestamp); } } Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(file); transformer.transform(source, result); }