1. 生成XML类:
public class WriteXml { String name = "http://www.w3.org/2001/XMLSchema-instance"; String value = "http://www.w3.org/2001/XMLSchema"; private XmlSerializer serializer; private StringWriter writer; private DevicesInfo devicesInfo; private String[] MeasurementData = { "Name", "Status", "Result", "LowerLimit", "UpperLimit", "Units", "CompType", "LogCount", "LogTime" }; public WriteXml(Context context) { writer = new StringWriter(); serializer = Xml.newSerializer(); // serializer = new FastXmlSerializer(); devicesInfo = new DevicesInfo(context); Test(); } public String createXmlFile() { try { serializer.setOutput(writer); serializer.startDocument("UTF-8", true); serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); serializer.startTag(null, "TestResults"); serializer.attribute(null, "xmlns:xsi", name); serializer.attribute(null, "xmlns:xsd", value); createAllResult(); createDevicesInfo(); serializer.startTag(null, "TestDatas"); Map<String, String> map0 = mList.get(0); Map<String, String> map1 = mList.get(1); createTestDatas(map0, "SAMPLER_SETUP_STATUS", "PASS", false); createTestDatas(map1, "ENUMERATION_STATUS", "PASS", false); createTestDatas(map1, "FUNCTIONS_TEST_RESULTS", "PASS", true); serializer.endTag(null, "TestDatas"); serializer.endTag(null, "TestResults"); serializer.endDocument(); return writer.toString(); } catch (Exception e) { throw new RuntimeException(e); } } private void createAllResult() { try { serializer.startTag(null, "OverallResult"); serializer.startTag(null, "Result"); serializer.text("PASS"); serializer.endTag(null, "Result"); serializer.startTag(null, "ErrorCode"); serializer.text("NoError"); serializer.endTag(null, "ErrorCode"); serializer.startTag(null, "ErrorMessage"); serializer.endTag(null, "ErrorMessage"); serializer.startTag(null, "ErrorTestName"); serializer.endTag(null, "ErrorTestName"); serializer.startTag(null, "ErrorFullTestName"); serializer.endTag(null, "ErrorFullTestName"); serializer.startTag(null, "ErrorDetails"); serializer.endTag(null, "ErrorDetails"); serializer.endTag(null, "OverallResult"); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void createDevicesInfo() { try { serializer.startTag(null, "UnitSerialNumber"); serializer.text(devicesInfo.getSerialNumber()); serializer.endTag(null, "UnitSerialNumber"); serializer.startTag(null, "StartDate"); serializer.text(devicesInfo.getCurrentTime()); serializer.endTag(null, "StartDate"); serializer.startTag(null, "StopDate"); serializer.text(devicesInfo.getCurrentTime()); serializer.endTag(null, "StopDate"); serializer.startTag(null, "ModelName"); serializer.text(devicesInfo.getModelName()); serializer.endTag(null, "ModelName"); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void createTestDatas(Map<String, String> map, String name, String status, boolean isloop) { try { serializer.startTag(null, "TestData"); serializer.attribute(null, "Name", name); serializer.attribute(null, "Status", status); serializer.startTag(null, "Measurements"); if (isloop) { for (int i = 0; i < 9; i++) { serializer.startTag(null, "MeasurementData"); createMeasurementData(map); serializer.endTag(null, "MeasurementData"); } } else { serializer.startTag(null, "MeasurementData"); createMeasurementData(map); serializer.endTag(null, "MeasurementData"); } serializer.endTag(null, "Measurements"); serializer.endTag(null, "TestData"); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void createMeasurementData(Map<String, String> map) { try { for (int i = 0; i < 9; i++) { serializer.attribute(null, MeasurementData[i], map.get(MeasurementData[i])); } } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } ArrayList<Map<String, String>> mList = new ArrayList<Map<String, String>>(); private void Test() { for (int j = 0; j < 2; j++) { Map<String, String> map = new HashMap<String, String>(); for (int i = 0; i < 9; i++) { map.put(MeasurementData[i], devicesInfo.getData(i)); } mList.add(map); } } }
2. 获取属性类:
public class DevicesInfo { private Context context; public DevicesInfo(Context context) { this.context = context; initData(); } public String getSerialNumber() { return android.os.Build.SERIAL; } public String getModelName() { return android.os.Build.MODEL; } public String getCurrentTime() { SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.ssssss"); return sDateFormat.format(new java.util.Date()); } // String[] data = {"STATUS","PASS","NoError","NoError","NoError","None","EQ","0",""}; String[] data = new String[9]; private void initData() { for (int i = 0; i < data.length; i++) { switch (i) { case 0: data[i] = "STATUS"; break; case 1: data[i] = "PASS"; break; case 2: data[i] = "NoError"; break; case 3: data[i] = "NoError"; break; case 4: data[i] = "NoError"; break; case 5: data[i] = "None"; break; case 6: data[i] = "EQ"; break; case 7: data[i] = "0"; break; case 8: data[i] = getCurrentTime(); break; } } } public String getData(int i){ return data[i]; } }
3.写成文件:
public boolean WriteFileData(String dirPath, String str) { OutputStream out = null; try { File f = new File(dirPath); out = new FileOutputStream(f); } catch (Exception e) { return false; } OutputStreamWriter outw = new OutputStreamWriter(out); try { outw.write(str); outw.close(); out.close(); } catch (Exception e) { return false; } return true; }