做一个远程备份和还原的项目,需要把一个xml文件的内容由web端发送给android手机端,然后手机端接受并解析。
由于还原功能是要读取备份数据,所以考虑将从手机端采集的数据备份到远程ftp,存在一个xml文件下。
然后还原过程是从ftp取出c.xml内容,存为String,然后插入到另一个a.xml文件中的一个子节点<data>下(下面红字标出),
a.xml内容如下:
<SyncMLs>
<SyncML xmlns='SYNCML:SYNCML1.2'>
<SyncHdr>
<VerDTD>1.2</VerDTD>
<VerProto>SyncML/1.2</VerProto>
<SessionID>1</SessionID>
<MsgID>1</MsgID>
<Target>
<LocURI>IMEI:[000000000000000, 000000000000000]</LocURI>
</Target>
<Source>
<LocURI>http://59.64.139.160:8080/NOMWEB/index.jsp</LocURI>
</Source>
</SyncHdr>
<cmd>RemoteRestore/2012-07-18 11:42:47</cmd>
<scmd>Get</scmd>
<scps>1</scps>
<imei>000000000000000</imei>
<sourceAddr>NMOTCGui</sourceAddr>
<data><Contacts>
<Contact>
<ciId>1</ciId>
<ciName>skiff Sun</ciName>
<ciPhoneNums>1-234-567-89;</ciPhoneNums>
<ciEmail>xiaoke2@126.com;</ciEmail>
<ciAddr>1-Network Manager -86-China -China -Beijing-Bupt</ciAddr>
</Contact>
<Contact>
<ciId>2</ciId>
<ciName>Tao Tao</ciName>
<ciPhoneNums>987-654-32100;1-234-567-8900;</ciPhoneNums>
<ciEmail>taoge@gmail.com;</ciEmail>
<ciAddr>1-null-100876-null-Beijing-Beijing-Wudaokou</ciAddr>
</Contact>
<Contact>
<ciId>3</ciId>
<ciName>Xiao Ke</ciName>
<ciPhoneNums>564-789-56244;1-580-000-0000;</ciPhoneNums>
<ciEmail>xiao2@126.com;</ciEmail>
<ciAddr>1-null-100876-null-BeiJing-BeiJing-GongZhuFen</ciAddr>
</Contact>
</Contacts>
</data>
</SyncML>
</SyncMLs>
其中红字部分为c.xml(也就是还原要取出的xml文件内容)
但是在解析xml文件取出数据时取出内容为null,代码如下
- Java code
DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.parse(openFileInput("cmd.xml")); Element rootElement = doc.getDocumentElement(); NodeList noteNodeList = rootElement.getElementsByTagName("SyncML"); for (int i = 0; i < noteNodeList.getLength(); i++) { String imei = doc.getElementsByTagName("imei").item(i) .getFirstChild().getNodeValue(); System.out.println(tm.getDeviceId()); if (imei.equals(tm.getDeviceId())) { cmd.setImei(imei); cmd.setSourceAddr(doc.getElementsByTagName("sourceAddr") .item(i).getFirstChild().getNodeValue()); // 还原数据功能使用,data用于存储从ftp取出的xml文件 cmd.setData(doc.getElementsByTagName("data").item(i) .getFirstChild().getNodeValue()); System.out.println(doc.getElementsByTagName("name")); System.out.println(doc.getElementsByTagName("name").item(0)); System.out.println(doc.getElementsByTagName("name").item(0).getFirstChild()); cmd.setCmd(doc.getElementsByTagName("cmd").item(i) .getFirstChild().getNodeValue()); cmd.setScps(doc.getElementsByTagName("scps") .item(i).getFirstChild().getNodeValue()); Log.i("cmd", cmd.getCmd()); // cmd.setCmdId(doc.getElementsByTagName("cmdId").item(i) // .getFirstChild().getNodeValue()); Log.i("cmdData:", cmd.getData()); // Toast.makeText(getBaseContext(), // "dm命令:"+cmd.getCmd()+"\n"+"命令参数:"+cmd.getData(), // Toast.LENGTH_SHORT).show();