xml文本格式是网络通信中最常用的格式,最近特别研究了一下如何解析xml文本并转换为对象,现在分享一下我最近的学习成果~@H_502_1@
先列一下本例中需要解析的xml文本:@H_502_1@
<resultsname="list">@H_502_1@
rowpubtime="2016-04-1316:40:13"author="APP"id="140"title="什么是公告"content="公告,是公开宣告。"/>@H_502_1@
rowpubtime="2016-04-1316:36:50"author="网站"id="138"title="12345678"content="12345678"rowpubtime="2016-04-0615:02:44"author="网站"id="134"title="关于网站用户注册流程说明1"content="关于用户注册流程说明"rowpubtime="2016-03-3018:32:13"author="APP"id="126"title="关于网站使用说明"content="测试"rowpubtime="2016-03-3018:29:26"author="网站"id="125"title="关于手机App使用说明"content="123"</results>@H_502_1@
@H_502_1@
讲一下我的思路,我选择使用XStream来解析xml文本,因为xstream在转换对象方面会比dom4j更优秀一些,它是通过注解方式来声明对应结点的,在操作上会更直观方便。首先会将整个文本转换成一个Results类对象,而每一个row结点作为一个HashMap放入到Results类对象的List列表中,最后会将每一个HashMap读取出来通过JAVA的反射机制转换为Info对象,并生成List列表。下载@H_502_1@
@H_502_1@
publicclassInfo{@H_502_1@
@H_502_1@
privateStringid;@H_502_1@
privateStringtitle;@H_502_1@
privateStringcontent;@H_502_1@
privateStringauthor;@H_502_1@
privateStringpubtime;@H_502_1@
publicStringgetId(){@H_502_1@
returnid;@H_502_1@
}@H_502_1@
publicvoidsetId(Stringid){@H_502_1@
this.id=id;@H_502_1@
}@H_502_1@
publicStringgetTitle(){@H_502_1@
returntitle;@H_502_1@
}@H_502_1@
publicvoidsetTitle(Stringtitle){@H_502_1@
this.title=title;@H_502_1@
}@H_502_1@
publicStringgetContent(){@H_502_1@
returncontent;@H_502_1@
}@H_502_1@
publicvoidsetContent(Stringcontent){@H_502_1@
this.content=content;@H_502_1@
}@H_502_1@
publicStringgetAuthor(){@H_502_1@
returnauthor;@H_502_1@
}@H_502_1@
publicvoidsetAuthor(Stringauthor){@H_502_1@
this.author=author;@H_502_1@
}@H_502_1@
publicStringgetPubtime(){@H_502_1@
returnpubtime;@H_502_1@
}@H_502_1@
publicvoidsetPubtime(Stringpubtime){@H_502_1@
this.pubtime=pubtime;@H_502_1@
}@H_502_1@
@Override@H_502_1@
publicStringtoString(){@H_502_1@
return"Info[author="+author+",content="+content+",id="+id@H_502_1@
+",pubtime="+pubtime+",title="+title+"]";@H_502_1@
}@H_502_1@
@H_502_1@
@H_502_1@
@H_502_1@
}@H_502_1@
@H_502_1@
Row类代码
@XStreamConverter(RowConverter.class) publicclassRowextendsHashMap<String,String>{ privatestaticfinallongserialVersionUID=5619951409573339302L; @H_502_1@
Results代码
@XStreamAlias("results") publicclassResults{ @XStreamAlias("name") @XStreamAsAttribute privateStringname; @XStreamImplicit(itemFieldName="row") privateList<Row>rows; publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicList<Row>getRows(){ returnrows; } publicvoidsetRows(List<Row>rows){ this.rows=rows; } }
Rowconverter类代码
publicclassRowConverterextendsAbstractCollectionConverter{ publicRowConverter(Mappermapper){ super(mapper); //TODOAuto-generatedconstructorstub } @Override publicbooleancanConvert(Classarg0){ //TODOAuto-generatedmethodstub returnRow.class.equals(arg0); } @Override@H_502_1@ publicvoidmarshal(Objectarg0,HierarchicalStreamWriterwriter,@H_502_1@ MarshallingContextarg2){@H_502_1@ //TODOAuto-generatedmethodstub@H_502_1@ Rowmap=(Row)arg0;@H_502_1@ for(Iteratoriterator=map.entrySet().iterator();iterator.hasNext();){@H_502_1@ Map.Entryentry=(Map.Entry)iterator.next();@H_502_1@ writer.addAttribute(entry.getKey().toString(),entry.getValue().toString());@H_502_1@ }@H_502_1@ }@H_502_1@ @H_502_1@ @Override@H_502_1@ publicObjectunmarshal(HierarchicalStreamReaderreader,@H_502_1@ UnmarshallingContextcontext){@H_502_1@ //TODOAuto-generatedmethodstub@H_502_1@ Rowmap=newRow();@H_502_1@ populateMap(reader,context,map);@H_502_1@ returnmap;@H_502_1@ }@H_502_1@ @H_502_1@ protectedvoidpopulateMap(HierarchicalStreamReaderreader,UnmarshallingContextcontext,Rowmap){@H_502_1@ Iterator<String>iterator=reader.getAttributeNames();@H_502_1@ while(iterator.hasNext()){@H_502_1@ Objectkey=iterator.next();@H_502_1@ Stringvalue=reader.getAttribute((String)key);@H_502_1@ map.put(key.toString(),value.toString());@H_502_1@ }@H_502_1@ }@H_502_1@ }@H_502_1@ RowConverter是一个转换器类,作用是将每一个row结点转变一个HashMap。@H_502_1@
publicclassXstream{@H_502_1@ @H_502_1@ privatestaticStringxml;@H_502_1@ @H_502_1@ staticvoidmain(String[]args)throwsException{@H_502_1@ //初始化@H_502_1@ init();@H_502_1@ @H_502_1@ XStreamxstream=newXStream(newXppDriver(newXmlFriendlyReplacer("_-","_")));@H_502_1@ //解析xml文本@H_502_1@ xstream.processAnnotations(Results.class);@H_502_1@ Resultsresults=(Results)xstream.fromXML(xml);@H_502_1@ //将解析出来的Results类对象转化成list列表@H_502_1@ List<Info>list=createList(Info.class,results);@H_502_1@ @H_502_1@ for(inti=0;i<list.size();i++){@H_502_1@ //打印@H_502_1@ Infoinfo=list.get(i);@H_502_1@ System.out.println(info.toString());@H_502_1@ }@H_502_1@ @H_502_1@ }@H_502_1@ voidinit(){@H_502_1@ //初始化xml文本@H_502_1@ xml="<resultsname=\"list\"><rowpubtime=\"2016-04-1316:40:13\"author=\"APP\"id=\"140\"title=\"什么是公告\"content=\"公告,是公开宣告。\"/><rowpubtime=\"2016-04-1316:36:50\"author=\"网站\"id=\"138\"title=\"12345678\"content=\"12345678\"/><rowpubtime=\"2016-04-0615:02:44\"author=\"网站\"id=\"134\"title=\"关于网站用户注册流程说明1\"content=\"关于用户注册流程说明\"/><rowpubtime=\"2016-03-3018:32:13\"author=\"APP\"id=\"126\"title=\"关于网站使用说明\"content=\"测试\"/><rowpubtime=\"2016-03-3018:29:26\"author=\"网站\"id=\"125\"title=\"关于手机App使用说明\"content=\"123\"/></results>";@H_502_1@ }@H_502_1@ static<T>ListcreateList(Class<T>clz,Resultsresults)throwsException{@H_502_1@ Listlist=newArrayList();@H_502_1@ for(Rowrow:results.getRows()){@H_502_1@ list.add(createObject(clz,row));@H_502_1@ @H_502_1@ }@H_502_1@ returnlist;@H_502_1@ }@H_502_1@ static<T>TcreateObject(Class<T>clazz,Rowrow)//初始化对象@H_502_1@ Tobj=clazz.newInstance();@H_502_1@ for(Methodmethod:clazz.getDeclaredMethods()){@H_502_1@ StringmethodName=method.getName();@H_502_1@ Class[]perams=method.getParameterTypes();@H_502_1@ if(methodName.startsWith("set")&&methodName.length()>3&&perams.length==1){@H_502_1@ @H_502_1@ Stringtemp=methodName.substring(3,methodName.length());@H_502_1@ StringfieldName=temp.toLowerCase();@H_502_1@ Stringvalue=row.get(fieldName);@H_502_1@ @H_502_1@ if(value!=null){@H_502_1@ ClassparamClass=perams[0];@H_502_1@ if(String.class.equals(paramClass)){@H_502_1@ method.invoke(obj,value);@H_502_1@ }elseif(Integer.class.equals(paramClass)||int.502 method.invoke(obj,Long.valueOf(value));@H_502_1@ }if(BigDecimal.newBigDecimal(value));@H_502_1@ }if(Boolean.boolean.class.equals(paramClass)){@H_502_1@ if(value.equals("true")||value.equals("TRUE"))@H_502_1@ method.invoke(obj,85);font-weight:bold;">true);@H_502_1@ if(value.equals("false")||value.equals("FALSE"))@H_502_1@ method.invoke(obj,85);font-weight:bold;">false);@H_502_1@ }@H_502_1@ }@H_502_1@ @H_502_1@ }@H_502_1@ }@H_502_1@ returnobj;@H_502_1@ }@H_502_1@ }@H_502_1@ Java代码
Info[author=APP,content=公告,是公开宣告。,id=140,pubtime=2016-04-1316:40:13,title=什么是公告]@H_502_1@ Info[author=网站,content=12345678,0);">138,0);">36:50,title=12345678]@H_502_1@ Info[author=网站,content=关于用户注册流程说明,0);">134,0);">0615:02:44,title=关于网站用户注册流程说明1]@H_502_1@ Info[author=APP,content=测试,0);">126,0);">03-3018:32:502 Info[author=网站,0);">123,0);">125,0);">29:26,title=关于手机App使用说明] @H_502_1@
_1@