转换xml格式的短信记录

来源:互联网 发布:长时间录像软件app 编辑:程序博客网 时间:2024/06/03 02:01

<?xml version="1.0" encoding="UTF-8"?>
<SMSRecord>
    <SMS>
        <Type>2</Type>
        <Status>0</Status>
        <IsRead>1</IsRead>
        <Address>18712345678</Address>
        <Date>2015-11-17 00:04:40</Date>
        <Subject></Subject>
        <Body>晚安 妞</Body>
    </SMS>
    <SMS>
        <Type>1</Type>
        <Status>0</Status>
        <IsRead>1</IsRead>
        <Address>18712345678</Address>
        <Date>2015-11-17 00:04:01</Date>
        <Subject></Subject>
        <Body>(&gt;^ω^&lt;)还好啦,你也早睡,晚安~</Body>
    </SMS>
    <SMS>
        <Type>2</Type>
        <Status>0</Status>
        <IsRead>1</IsRead>
        <Address>18712345678</Address>
        <Date>2015-11-17 00:03:23</Date>
        <Subject></Subject>
        <Body>哈哈 想名字这么高难度的事 还是交给你吧 另外 赶紧睡觉! 你今天也是累了一天了</Body>
    </SMS>

</SMSRecord>

今天把手机里和老婆的短信记录导出了,是xml格式的,如上所示

type是2的表示是我自己发出去的

type是1的表示是媳妇发给我的

这种格式的看着有点费劲呀,看着不爽呀!

于是就有了下面的代码



package prac;import java.io.*;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;public class XMLReaderTest {public static void main(String args[]) {Element element = null;// 可以使用绝对路劲/Practice/src/prac/File f = new File("src/prac/Sms_QQPhoneManager(2015-11-18).xml");// documentBuilder为抽象不能直接实例化(将XML文件转换为DOM文件)DocumentBuilder db = null;DocumentBuilderFactory dbf = null;try {// 返回documentBuilderFactory对象dbf = DocumentBuilderFactory.newInstance();// 返回db对象用documentBuilderFatory对象获得返回documentBuildr对象db = dbf.newDocumentBuilder();// 得到一个DOM并返回给document对象Document dt = db.parse(f);// 得到一个elment根元素element = dt.getDocumentElement();// 获得根节点System.out.println("根元素:" + element.getNodeName());// 获得根元素下的子节点NodeList childNodes = element.getChildNodes();// 遍历这些子节点List<Msg> msgs=new ArrayList<>();Msg msg=null;for (int i = 0; i < childNodes.getLength(); i++) {msg=new Msg();// 获得每个对应位置i的结点Node node1 = childNodes.item(i);if ("SMS".equals(node1.getNodeName())) {// 获得<Accounts>下的节点NodeList nodeDetail = node1.getChildNodes();// 遍历<Accounts>下的节点for (int j = 0; j < nodeDetail.getLength(); j++) {// 获得<Accounts>元素每一个节点Node detail = nodeDetail.item(j);if ("Type".equals(detail.getNodeName())) {if (detail.getTextContent().equals("2")) {msg.setName("我");}else {msg.setName("老婆");}}if ("Date".equals(detail.getNodeName())) msg.setDate(detail.getTextContent());if ("Body".equals(detail.getNodeName())) msg.setText(detail.getTextContent());}msgs.add(msg);}}//为什么要逆序 大家明白了么?for (int i=msgs.size()-1;i>-1 ; i--) {System.out.println(msgs.get(i).getName()+" "+msgs.get(i).getDate()+"  "+msgs.get(i).getText());}} catch (Exception e) {e.printStackTrace();}}}class Msg{private String name;private String date;private String text;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getDate() {return date;}public void setDate(String date) {this.date = date;}public String getText() {return text;}public void setText(String text) {this.text = text;}}

后来觉得麻烦就改成了下面这个样子:

public static void main(String args[]) {Element element = null;// 可以使用绝对路劲/Practice/src/prac/File f = new File("src/prac/Sms_QQPhoneManager(2015-11-18).xml");// documentBuilder为抽象不能直接实例化(将XML文件转换为DOM文件)DocumentBuilder db = null;DocumentBuilderFactory dbf = null;try {// 返回documentBuilderFactory对象dbf = DocumentBuilderFactory.newInstance();// 返回db对象用documentBuilderFatory对象获得返回documentBuildr对象db = dbf.newDocumentBuilder();// 得到一个DOM并返回给document对象Document dt = db.parse(f);// 得到一个elment根元素element = dt.getDocumentElement();// 获得根节点System.out.println("根元素:" + element.getNodeName());// 获得根元素下的子节点NodeList childNodes = element.getChildNodes();// 遍历这些子节点List<Msg> msgs=new ArrayList<>();Msg msg=null;int length=childNodes.getLength();//这length 并不是来往短信的条目数for (int i = 0; i < length; i++) {msg=new Msg();// 获得每个对应位置i的结点Node node1 = childNodes.item(i);if ("SMS".equals(node1.getNodeName())) {// 获得<Accounts>下的节点NodeList nodeDetail = node1.getChildNodes();//nodeDetail里就是Type date等等//一条短信有type body等7个字段 但是nodeDetail.getLength()并不等于7msg.setDate(nodeDetail.item(9).getTextContent());if (nodeDetail.item(1).getTextContent().equals("2")) {msg.setName("我");}else {msg.setName("老婆");}msg.setText(nodeDetail.item(13).getTextContent());msgs.add(msg);}}//为什么要逆序 大家明白了么?for (int i=msgs.size()-1;i>-1 ; i--) {System.out.println(msgs.get(i).getName()+" "+msgs.get(i).getDate()+"  "+msgs.get(i).getText());}} catch (Exception e) {e.printStackTrace();}}

运行结果


我 2015-11-17 00:03:23  哈哈 想名字这么高难度的事 还是交给你吧 另外 赶紧睡觉! 你今天也是累了一天了老婆 2015-11-17 00:04:01  (>^ω^<)还好啦,你也早睡,晚安~我 2015-11-17 00:04:40  晚安 妞


0 0
原创粉丝点击