一、新建实体bean,将xml文件转换为ThdReport与image列表
-
package com.etoak.bean; -
import java.util.List; -
public class ThdReport { -
private Long id; -
private Short type; -
private String sn; -
private String title; -
private String name; -
private String gender; -
private String examDate; -
private String birthday; -
private Long patientId; -
private String conclusion; //Summary检查结果 -
private String conclusionDesc;//Description检查描述 -
private String operatorPhysician; -
private String performingPhysician; -
private String referringPhysician; -
private String age; -
private List<Image> images; -
public List<Image> getImages() { -
return images; -
} -
public void setImages(List<Image> images) { -
images = images; -
} -
public Long getId() { -
return id; -
} -
public void setId(Long id) { -
this.id = id; -
} -
public Short getType() { -
return type; -
} -
public void setType(Short type) { -
this.type = type; -
} -
public String getSn() { -
return sn; -
} -
public void setSn(String sn) { -
this.sn = sn; -
} -
public String getTitle() { -
return title; -
} -
public void setTitle(String title) { -
this.title = title; -
} -
public String getName() { -
return name; -
} -
public void setName(String name) { -
this.name = name; -
} -
public String getGender() { -
return gender; -
} -
public void setGender(String gender) { -
this.gender = gender; -
} -
public String getExamDate() { -
return examDate; -
} -
public void setExamDate(String examDate) { -
this.examDate = examDate; -
} -
public String getBirthday() { -
return birthday; -
} -
public void setBirthday(String birthday) { -
this.birthday = birthday; -
} -
public Long getPatientId() { -
return patientId; -
} -
public void setPatientId(Long patientId) { -
this.patientId = patientId; -
} -
public String getConclusion() { -
return conclusion; -
} -
public void setConclusion(String conclusion) { -
this.conclusion = conclusion; -
} -
public String getConclusionDesc() { -
return conclusionDesc; -
} -
public void setConclusionDesc(String conclusionDesc) { -
this.conclusionDesc = conclusionDesc; -
} -
public String getOperatorPhysician() { -
return operatorPhysician; -
} -
public void setOperatorPhysician(String operatorPhysician) { -
this.operatorPhysician = operatorPhysician; -
} -
public String getPerformingPhysician() { -
return performingPhysician; -
} -
public void setPerformingPhysician(String performingPhysician) { -
this.performingPhysician = performingPhysician; -
} -
public String getReferringPhysician() { -
return referringPhysician; -
} -
public void setReferringPhysician(String referringPhysician) { -
this.referringPhysician = referringPhysician; -
} -
public String getAge() { -
return age; -
} -
public void setAge(String age) { -
this.age = age; -
} -
@Override -
public String toString() { -
return "ThdReport [id=" + id + ", type=" + type + ", sn=" + sn -
+ ", title=" + title + ", name=" + name + ", gender=" + gender -
+ ", examDate=" + examDate + ", birthday=" + birthday -
+ ", patientId=" + patientId + ", conclusion=" + conclusion -
+ ", conclusionDesc=" + conclusionDesc + ", operatorPhysician=" -
+ operatorPhysician + ", performingPhysician=" -
+ performingPhysician + ", referringPhysician=" -
+ referringPhysician + ", age=" + age + ", Images=" + images -
+ "]"; -
} -
}
-
package com.etoak.bean; -
public class Image { -
private String path; -
private String type; -
private String content; -
public String getPath() { -
return path; -
} -
public void setPath(String path) { -
this.path = path; -
} -
public String getType() { -
return type; -
} -
public void setType(String type) { -
this.type = type; -
} -
public String getContent() { -
return content; -
} -
public void setContent(String content) { -
this.content = content; -
} -
@Override -
public String toString() { -
return "Image [path=" + path + ", type=" + type + ", content=" -
+ content + "]"; -
} -
}
二、编写Image转换器,因为xml中的Image中既有属性又有内容
-
package com.etoak.test; -
import com.etoak.bean.Image; -
import com.thoughtworks.xstream.converters.Converter; -
import com.thoughtworks.xstream.converters.MarshallingContext; -
import com.thoughtworks.xstream.converters.UnmarshallingContext; -
import com.thoughtworks.xstream.io.HierarchicalStreamReader; -
import com.thoughtworks.xstream.io.HierarchicalStreamWriter; -
public class XStreamExpandConverter implements Converter{ -
@Override -
public boolean canConvert(Class type) { -
return type.equals(Image.class); -
} -
@Override -
public Object unmarshal(HierarchicalStreamReader reader, -
UnmarshallingContext context) { -
Image s = new Image(); -
s.setPath(reader.getAttribute("Path")); -
s.setType(reader.getAttribute("Type")); -
s.setContent(reader.getValue()); -
return s; -
} -
@Override -
public void marshal(Object source, HierarchicalStreamWriter writer, -
MarshallingContext context) { -
Image status = (Image) source; -
writer.addAttribute("Path", status.getPath()); -
writer.addAttribute("Type", status.getType()); -
writer.setValue(status.getContent()); -
} -
}
三、编写日期转换器,因为日期在xstream中不能直接转换为yyyy-MM-dd hh:mm:ss格式
-
package com.etoak.test; -
import java.text.DateFormat; -
import java.text.ParseException; -
import java.text.SimpleDateFormat; -
import java.util.Date; -
import com.thoughtworks.xstream.converters.ConversionException; -
import com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter; -
public class XStreamYMDDateConverter extends AbstractSingleValueConverter { -
private static final DateFormat DEFAULT_DATEFORMAT = new SimpleDateFormat( -
"yyyy-MM-dd hh:mm:ss"); -
@Override -
public boolean canConvert(Class type) { -
return type.equals(Date.class); -
} -
@Override -
public Object fromString(String str) { -
// 这里将字符串转换成日期 -
try { -
return DEFAULT_DATEFORMAT.parseObject(str); -
} catch (ParseException e) { -
e.printStackTrace(); -
} -
throw new ConversionException("Cannot parse datedd " + str); -
} -
@Override -
public String toString(Object obj) { -
// 这里将日期转换成字符串 -
return DEFAULT_DATEFORMAT.format((Date) obj); -
} -
}
四、编写测试类
-
package com.etoak.test; -
import java.io.IOException; -
import java.io.ObjectInputStream; -
import java.io.ObjectOutputStream; -
import java.util.List; -
import org.springframework.context.ApplicationContext; -
import org.springframework.context.support.ClassPathXmlApplicationContext; -
import com.etoak.bean.Image; -
import com.etoak.bean.ThdReport; -
import com.thoughtworks.xstream.XStream; -
public class Test { -
private XStream xstream = null; -
private ThdReport report = null; -
@org.junit.Test -
public void readXML4InputStream() { -
try { -
xstream = new XStream(); -
String s = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" -
+"<ReportInfo>" -
+"<Title></Title>" -
+"<PatientId>320525198109110058</PatientId>" -
+"<Name>吴志平</Name>" -
+"<Gender>true</Gender>" -
+"<Age>0</Age>" -
+"<ExamDate>16-01-08 18:25:48</ExamDate>" -
+"<Birthday>81-09-11 00:00:00</Birthday>" -
+"<Description>动脉血管内膜回声增强,增厚,有中断现象。于血管前、后壁可见斑快回声(1,为软斑呈弱回声或等回声2,为硬斑回声增强,后方有声影),团块回声附着于血管壁上。" -
+"彩色血流于动脉处出现充盈缺损,血流变细,色彩明显。于狭窄的近端有明亮及增宽的高速血流。提示有侧支循环建立。 " -
+"频谱多普勒于狭窄处取样收缩期峰值加快达 cm/s,舒张期反向血流消失频谱增宽,病变远端动脉,峰值速度、平均速度均减慢,呈低幅、单相血流。" -
+"频谱:有峰值后移现象</Description>" -
+"<Summary>动脉管壁正常结构消失,呈不规则向心性增厚,最厚达mm,回声不均匀,管腔狭窄,最窄处为 mm。" -
+"彩色血流于动脉处变细,出现五彩镶嵌或混叠现象(或彩色血流中断)。" -
+"频谱多普勒于病变处动脉取样,血流速度明显加快,频带增宽,频窗减小(或消失),狭窄远端血流频谱为低阻力、低速,单相血流," -
+"动脉呈梭状(或囊状)扩张,内壁回声增强,不光滑。动脉瘤长:mm,宽:mm,开口mm。彩色血流于瘤体内呈红、蓝各半血流(旋流),此血流与动脉干内血流相连</Summary>" -
+"<OperatorPhysician></OperatorPhysician>" -
+"<PerformingPhysician>VINNOTester</PerformingPhysician>" -
+"<ReferringPhysician>VINNOTester</ReferringPhysician>" -
+"<Images>" -
+"<Image Path=\"E9352D9A4212449F91E54A3980220691\" Type=\"1\">iVBORw0KGgoAAAANSUhEUgOVbWd//f2f671nr6</Image>" -
+"<Image Path=\"87EFA807B0C84090B7001D72EB60F0F3\" Type=\"1\">iVBORw0KGgoAAAANSUhEUgAABOEAAALqCAYAAABzMk</Image>" -
+"<Image Path=\"D975EFD027CF4672A8CA6B895A10159A\" Type=\"1\">iVBORw0KCCEML/BxnYyYrfKXeUAAAAAElFTkSuQmCC</Image>" -
+"<Image Path=\"1ED902F498094A6496128594BE31B254\" Type=\"3\">iVBORw0KGgoAAAANSUhEUgAAgT0r9P/ug49yMtk8jAAAAAElFTkSuQmCC</Image>" -
+"</Images>" -
+"</ReportInfo>"; -
failRed("---------ObjectInputStream## XML --> javaObject---------"); -
xstream.alias("ReportInfo",ThdReport.class); -
xstream.aliasField("Title", ThdReport.class, "title"); -
xstream.aliasField("Name", ThdReport.class, "name"); -
xstream.aliasField("Gender", ThdReport.class, "gender"); -
xstream.aliasField("ExamDate", ThdReport.class, "examDate"); -
xstream.aliasField("Birthday", ThdReport.class, "birthday"); -
xstream.aliasField("PatientId", ThdReport.class, "patientId"); -
xstream.aliasField("Summary", ThdReport.class, "conclusion"); -
xstream.aliasField("Description", ThdReport.class, "conclusionDesc"); -
xstream.aliasField("OperatorPhysician", ThdReport.class, "operatorPhysician"); -
xstream.aliasField("PerformingPhysician", ThdReport.class, "performingPhysician"); -
xstream.aliasField("ReferringPhysician", ThdReport.class, "referringPhysician"); -
xstream.aliasField("Age", ThdReport.class, "age"); -
xstream.aliasField("Images", ThdReport.class, "images"); -
xstream.alias("Images", List.class); -
xstream.alias("Image", Image.class); -
xstream.registerConverter(new XStreamYMDDateConverter()); -
xstream.registerConverter(new XStreamExpandConverter()); -
ThdReport report = (ThdReport) xstream.fromXML(s); -
System.out.println(report); -
} catch (Exception e) { -
e.printStackTrace(); -
} -
} -
public final void fail(String string) { -
System.out.println(string); -
} -
public final void failRed(String string) { -
System.err.println(string); -
} -
}
五、简单介绍
-
alias(String 别名,Class class)方法给转换类起个别名,当xml中要转换为类的标签和类名不一致(包+类名都一致才为一致)时使用 -
aliasField(String 别名, Class class, String 属性名)
-
registerConverter(new 转换器)
六、下面是别人的xStream介绍可以参考下
http://www.cnblogs.com/hoojo/archive/2011/04/22/2025197.html
http://www.blogjava.net/DLevin/archive/2012/11/30/392240.html
XStream解析和读取xml报文
XStream处理既有属性又有值的xml节点
xstream转换Map和List
https://blog.youkuaiyun.com/just_you_java/article/details/50971208
1855

被折叠的 条评论
为什么被折叠?



