微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

XML解析小记之防止数据过长被截断

public class AppWebSaxXml extends DefaultHandler { private StringBuilder sb = new StringBuilder(); private AppWebEntity appEntity; private List<AppWebEntity> appEntitys; private String preTag; @Override public void startDocument() throws SAXException { appEntitys = new ArrayList<AppWebEntity>(); } @Override public void characters(char[] ch,int start,int length) throws SAXException { //(2)不管在startElement到endElement的过程中,执行了多少次characters, 都会将内容添加到StringBuilder中,不会丢失内容 sb.append(ch,start,length); } @Override public void startElement(String uri,String localName,String name,Attributes attr) throws SAXException { //(3) 开始收集新的标签的数据时,先清空历史数据 sb.setLength(0); if ("Message".equals(name)) { appEntity=new AppWebEntity(); } preTag=name; } @Override public void endElement(String uri,String name) throws SAXException { if(appEntity!=null){ String data = sb.toString(); if ("imsI".equals(preTag)) { appEntity.setPhoneInfo_imsI(data); } if ("MEID".equals(preTag)) { appEntity.setPhoneInfo_MEID(data); } if ("PhoneType".equals(preTag)) { appEntity.setPhoneInfo_PhoneType(data); } if ("Osversion".equals(preTag)) { appEntity.setPhoneInfo_Osversion(data); } if ("BaseBand".equals(preTag)) { appEntity.setPhoneInfo_BaseBand(data); } if ("Kernel".equals(preTag)) { appEntity.setPhoneInfo_Kernel(data); } if ("InnerVersion".equals(preTag)) { appEntity.setPhoneInfo_InnerVersion(data); } if ("RamUsage".equals(preTag)) { appEntity.setPhoneInfo_RamUsage(data); } if ("cpuUsage".equals(preTag)) { appEntity.setPhoneInfo_cpuUsage(data); } if ("Longitude".equals(preTag)) { appEntity.setPositionInfo_Longitude(data); } if ("Latitude".equals(preTag)) { appEntity.setPositionInfo_Latitude(data); } if ("LocationDesc".equals(preTag)) { appEntity.setPositionInfo_LocationDesc(data); } if ("Province".equals(preTag)) { appEntity.setPositionInfo_Province(data); } if ("City".equals(preTag)) { appEntity.setPositionInfo_City(data); } if ("NetType".equals(preTag)) { appEntity.setNetInfo_NetType(data); } if ("APN".equals(preTag)) { appEntity.setNetInfo_APN(data); } if ("CdmaSid".equals(preTag)) { appEntity.setNetInfo_CdmaSid(data); } if ("CdmaNid".equals(preTag)) { appEntity.setNetInfo_CdmaNid(data); } if ("CdmaBsid".equals(preTag)) { appEntity.setNetInfo_CdmaBsid(data); } if ("CdmadBm".equals(preTag)) { appEntity.setNetInfo_CdmadBm(data); } if ("LteCi".equals(preTag)) { appEntity.setNetInfo_LteCi(data); } if ("LtePci".equals(preTag)) { appEntity.setNetInfo_LtePci(data); } if ("LteTac".equals(preTag)) { appEntity.setNetInfo_LteTac(data); } if ("LteRsrp".equals(preTag)) { appEntity.setNetInfo_LteRsrp(data); } if ("LteSinr".equals(preTag)) { appEntity.setNetInfo_LteSinr(data); } if ("InnerIP".equals(preTag)) { appEntity.setNetInfo_InnerIP(data); } if ("OuterIP".equals(preTag)) { appEntity.setNetInfo_OuterIP(data); } if ("WebsiteName".equals(preTag)) { appEntity.setTestResult_WebsiteName(data); } if ("PageURL".equals(preTag)) { appEntity.setTestResult_PageURL(data); } if ("PageIP".equals(preTag)) { appEntity.setTestResult_PageIP(data); } if ("PageSurftime".equals(preTag)) { appEntity.setTestResult_PageSurftime(data); } if ("FirstByteDelay".equals(preTag)) { appEntity.setTestResult_FirstByteDelay(data); } if ("PageOpenDelay".equals(preTag)) { appEntity.setTestResult_PageOpenDelay(data); } if ("RRCSetupDelay".equals(preTag)) { appEntity.setTestResult_RRCSetupDelay(data); } if ("DnsDelay".equals(preTag)) { appEntity.setTestResult_DnsDelay(data); } if ("ConnDelay".equals(preTag)) { appEntity.setTestResult_ConnDelay(data); } if ("ReqDelay".equals(preTag)) { appEntity.setTestResult_ReqDelay(data); } if ("ResDelay".equals(preTag)) { appEntity.setTestResult_ResDelay(data); } if ("TCLASS".equals(preTag)) { appEntity.setTestResult_TCLASS(data); } if ("Success".equals(preTag)) { appEntity.setTestResult_Success(data); } } if ("Message".equals(name)) { appEntitys.add(appEntity); appEntity=null; } preTag = null; } public List<AppWebEntity> getList() { // Todo Auto-generated method stub return appEntitys; } }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类