Back to project page AndroidHttpClient3.
The source code is released under:
MIT License
If you think the Android project AndroidHttpClient3 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.xmlReader; /*from w w w .j a v a2s .c o m*/ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xmlpull.v1.XmlSerializer; import android.util.Xml; import com.entity.Course; public class DomXMLReader { public static List<Course> readXml(InputStream inStream) { List<Course> courses = new ArrayList<Course>(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = factory.newDocumentBuilder(); Document dom = builder.parse(inStream); Element root = dom.getDocumentElement(); NodeList items = root.getElementsByTagName("course"); for(int i = 0; i < items.getLength(); i++) { Course course = new Course(); Element courseNode = (Element)items.item(i); course.setId(courseNode.getAttribute("id")); NodeList childsNodes = courseNode.getChildNodes(); for(int j = 0; j < childsNodes.getLength(); j++) { Node node = (Node)childsNodes.item(j); if(node.getNodeType() == Node.ELEMENT_NODE) { Element childNode = (Element)node; if("Monday".equals(childNode.getNodeName())) { course.setMonday(childNode.getFirstChild().getNodeValue()); } if("Tuesday".equals(childNode.getNodeName())) { course.setTuesday(childNode.getFirstChild().getNodeValue()); } if("Wednesday".equals(childNode.getNodeName())) { course.setWednesday(childNode.getFirstChild().getNodeValue()); } if("Thursday".equals(childNode.getNodeName())) { course.setThursday(childNode.getFirstChild().getNodeValue()); } if("Friday".equals(childNode.getNodeName())) { course.setFriday(childNode.getFirstChild().getNodeValue()); } if("Saturday".equals(childNode.getNodeName())) { course.setSaturday(childNode.getFirstChild().getNodeValue()); } if("Sunday".equals(childNode.getNodeName())) { course.setSunday(childNode.getFirstChild().getNodeValue()); } } } courses.add(course); inStream.close(); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return courses; } public static boolean saveAsXml(List<Course> courses, OutputStream outStream) { try { XmlSerializer serializer = Xml.newSerializer(); serializer.setOutput(outStream, "UTF-8"); serializer.startDocument("UTF-8", true); serializer.startTag(null, "courses"); for(Course course : courses) { serializer.startTag(null, "course"); serializer.attribute(null, "id", course.getId()); serializer.startTag(null, "Monday"); serializer.text(course.getMonday()); serializer.endTag(null, "Monday"); serializer.startTag(null, "Tuesday"); serializer.text(course.getTuesday()); serializer.endTag(null, "Tuesday"); serializer.startTag(null, "Wednesday"); serializer.text(course.getWednesday()); serializer.endTag(null, "Wednesday"); serializer.startTag(null, "Thursday"); serializer.text(course.getThursday()); serializer.endTag(null, "Thursday"); serializer.startTag(null, "Friday"); serializer.text(course.getFriday()); serializer.endTag(null, "Friday"); serializer.startTag(null, "Saturday"); serializer.text(course.getSaturday()); serializer.endTag(null, "Saturday"); serializer.startTag(null, "Sunday"); serializer.text(course.getSunday()); serializer.endTag(null, "Sunday"); serializer.endTag(null, "course"); } serializer.endTag(null, "courses"); serializer.endDocument(); outStream.flush(); outStream.close(); return true; } catch (Exception e) { // TODO: handle exception return false; } } public static void main(String args[]) { try { File f = new File("D://MyCouses.xml"); InputStream istream = new FileInputStream(f); List coursesList = DomXMLReader.readXml(istream); for(int i = 0; i < coursesList.size(); i++) { Course c = (Course)coursesList.get(i); System.out.println(c.getMonday()); System.out.println(c.getTuesday()); System.out.println(c.getWednesday()); System.out.println(c.getThursday()); System.out.println(c.getFriday()); System.out.println(c.getSaturday()); System.out.println(c.getSunday()); } } catch (IOException e) { // TODO: handle exception } } }