首页 技术 正文
技术 2022年11月16日
0 收藏 336 点赞 3,444 浏览 2480 个字

创建Girl.java类

import java.util.List;
import javax.xml.bind.annotation.*;@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "root")
public class Girl {
@XmlAttribute(name = "type")
private String type;
@XmlElement(name = "name")
private String name; @XmlElement(name = "age")
private String age; @XmlElement(name = "girl")
private List<Girl> girlList; public String toString() {
StringBuilder sb = new StringBuilder();
for (Girl girl : girlList) {
sb.append(girl.toString());
}
return sb.toString();
} public Girl(){
super();
} public Girl(String name,String age){
this.name=name;
this.age=age;
} public String getType() {
return type;
} public void setType(String type) {
this.type = type;
} public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public List<Girl> getGirlList() {
return girlList;
} public void setGirlList(List<Girl> girlList) {
this.girlList = girlList;
}}

  编写测试类

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;public class TestUnm {
public static void main(String[] args) {
marshaller();
} public static void marshaller() {
try {
JAXBContext jaxbC = JAXBContext.newInstance(Girl.class);
Marshaller ms = jaxbC.createMarshaller();
ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
Girl girl=new Girl();
List<Girl> girls=new ArrayList<Girl>();
Girl g1=new Girl("小红", "20");
Girl g2=new Girl("小芳", "16");
Girl g3=new Girl("小丽", "17");
girls.add(g1);
girls.add(g2);
girls.add(g3);
girl.setGirlList(girls);
StringWriter sw=new StringWriter();
ms.marshal(girl, sw);
System.out.println(sw.toString());
createFile("E:\\app\\","test.xml",sw.toString());
} catch (Exception e) {
e.printStackTrace();
}
} public static boolean createFile(String path,String fileName,String fileContent){
boolean bool=false;
File file=new File(path+File.separator+fileName);
try {
if(!file.exists()){
file.createNewFile();
bool=true;
}
writeFileContent(path+File.separator+fileName,fileContent);
} catch (Exception e) {
e.printStackTrace();
}
return bool;
} public static boolean writeFileContent(String path,String fileContent){
boolean bool=false;
FileOutputStream fos=null;
PrintWriter pw=null;
try {
File file=new File(path);
fos=new FileOutputStream(file);
OutputStreamWriter writer=new OutputStreamWriter(fos, "UTF-8");
pw=new PrintWriter(writer);
pw.write(fileContent);
pw.flush();
bool=true;
} catch (Exception e) {
e.printStackTrace();
}
return bool;
}}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,076
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,400
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,812
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,894