响尾蛇的gravatar头像
响尾蛇 2015-11-19 18:44:01

什么工具能最方便地由java类生成xml文件?

将一堆要传输的报文信息生成xml格式字符串传输,什么工具用的比较多

所有回答列表(5)
RSS订阅问答的gravatar头像
RSS订阅问答  LV2 2015年11月19日

http://www.everycoding.com/category/6.html

这里是对象和xml的文章集合,希望对你有帮助

评论(1) 最佳答案
最代码官方的gravatar头像
最代码官方  LV167 2015年11月19日

之前我分享过,你参考下:

java通过JAXB框架转换xml为java bean对象代码片段分享

其他相关源码:

java操作xml文件

遇见,的gravatar头像
遇见,  LV36 2015年11月19日

XmlSerializer

larslee的gravatar头像
larslee  LV1 2015年11月19日

百度不就有了http://zhidao.baidu.com/link?url=aly1utrmvC33yOgC4aNEw98Ou9GdjL9n8uJuq1lW_2Ubx2EF-lvwkIYk2Ndi_-j-GkyYXeujq0V_k71MitSMnK

响尾蛇的gravatar头像
响尾蛇  LV15 2015年11月20日

用XStream写个demo给大家看看:

maven依赖:

<dependency>
	    <groupId>com.thoughtworks.xstream</groupId>
	    <artifactId>xstream</artifactId>
	    <version>1.3.1</version>
	</dependency>
 
	<dependency>
	    <groupId>xpp3</groupId>
	    <artifactId>xpp3</artifactId>
	    <version>1.1.4c</version>
	</dependency>

三个类:

Student.java

package com.simpco.xstreamtest;

import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("Student")
public class Student {
	private String name;
	private String sex;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Student() {
		super();
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", sex=" + sex + "]";
	}
}

Root.java

package com.simpco.xstreamtest;

import java.util.ArrayList;
import java.util.List;

import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("Root")
public class Root {
        @XStreamAlias("StudentList")
	private List<Student> studentList;

	public List<Student> getStudentList() {
		return studentList;
	}

	public void setStudentList(List<Student> studentList) {
		this.studentList = studentList;
	}
	
	public void addStudent(Student student){
		if(studentList==null){
			studentList = new ArrayList<Student>();
		}
		studentList.add(student);
	}
}

运行 App.java


package com.simpco.xstreamtest;

import com.thoughtworks.xstream.XStream;

/**
 * Hello world!
 *
 */
public class App {
    public static void main( String[] args ){
    	XStream xstream = new XStream(); 
        Root root = new Root();
        Student student = new Student();
        student.setName("张三");
        student.setSex("男");
        root.addStudent(student);
        Student student1 = new Student();
        student1.setName("李四");
        student1.setSex("女");
        root.addStudent(student1);
//        xstream.alias("root", Root.class); 
//        xstream.alias("Student", Student.class);
//        Annotations.configureAliases(xstream, Student.class);
        xstream.processAnnotations(Root.class);
        xstream.processAnnotations(Student.class);//声明使用Student中的注解别名
        String xml = xstream.toXML(root);
        String top = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
        System.out.println(top+xml);
        
//        Root rootObj = (Root)xstream.fromXML(xml);
//        System.out.println(JSON.toJSONString(rootObj));
    }
}

预期结果:

<?xml version="1.0" encoding="UTF-8"?>
<Root>
  <StudentList>
    <Student>
      <name>张三</name>
      <sex>男</sex>
    </Student>
    <Student>
      <name>李四</name>
      <sex>女</sex>
    </Student>
  </StudentList>
</Root>
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友