Home  >  Article  >  Java  >  XML document conversion method implemented in Javabean based on xstream package

XML document conversion method implemented in Javabean based on xstream package

黄舟
黄舟Original
2017-05-28 09:19:001507browse

This article mainly introduces the Javabean method of converting XML documents based on the xstream package. It analyzes the specific usage skills of the xstream package for converting xml files based on specific examples. Friends in need can refer to the following

The example in this article describes how Javabean converts XML documents based on the xstream package. Share it with everyone for your reference, the details are as follows:

1. Required Jar package:

xpp3_min.jar
xstream.jar

This siteDownload address.

Add these two jars to the project

2. Add two javabeans for testing: Province and City

class Province
{
  private String name;// 省名
  private List<City> cities = new ArrayList<City>();
  public String getName()
  {
    return name;
  }
  public void setName(String name)
  {
    this.name = name;
  }
  public List<City> getCities()
  {
    return cities;
  }
  public void setCities(List<City> cities)
  {
    this.cities = cities;
  }
  public void addCity(City city)
  {
    cities.add(city);
  }
}
class City
{
  private String name;// 市名
  private String description;// 描述
  private String[] counties; // 该市的所有县
  public String getName()
  {
    return name;
  }
  public void setName(String name)
  {
    this.name = name;
  }
  public String[] getCounties()
  {
    return this.counties;
  }
  public void setCounties(String[] countries)
  {
    this.counties = countries;
  }
  public String getDescription()
  {
    return description;
  }
  public void setDescription(String description)
  {
    this.description = description;
  }
  @Override
  public String toString()
  {
    return "City [name=" + name + ", description=" + description + "]";
  }
  public City()
  {
    super();
  }
  public City(String name, String description)
  {
    super();
    this.name = name;
    this.description = description;
  }
}

XStream The method toXML(Object obj) is provided to return the generated xml document in the form of string. The
XStream#alias() method can change the Tag name of the generated element. If the Tag name is not set, the String form of Class will be used as the Tag name, such as: Java.lang.String.

XStream#useAttributeFor() method can convert Javabean's attribute into the attribute of xml element.

XStream#addImplicitCollection() method can convert collection elements in Javabeans, such as List, into each element directly into a sub-element without generating another element to serve as these sub-elements. parent element.

XStream#addImplicitArray() method is the same as addImplicitCollection(), except that the collection becomes an array.

The following is the test code:


package com.song.demo;
import java.util.ArrayList;
import java.util.List;
import com.thoughtworks.xstream.XStream;
public class BeanToXml
{
  public static void main(String[] args)
  {
    // 创建一个JavaBean
    List<Province> proList = new ArrayList<Province>();
    // 广东省
    Province gdProvince = new Province();
    gdProvince.setName("广东省");
    City gzCity = new City("广州市", "广东省省会");
    gzCity.setCounties(new String[]{"海珠区", "天河区", "白云区"});
    gdProvince.addCity(gzCity);
    gdProvince.addCity(new City("韶关", "粤北城市"));
    // 北京市
    Province bjProvince = new Province();
    bjProvince.setName("北京市");
    bjProvince.addCity(new City("东城区", "北京市东城区"));
    bjProvince.addCity(new City("西城区", "北京市西城区"));
    proList.add(gdProvince);
    proList.add(bjProvince);
    // 使用默认的方式生成xml
    // System.out.println(toXML1(proList));
    // 处理JavaBean中的集合属性,直接生成子节点
    System.out.println(toXml2(proList));
  }
  private static String toXml2(Object obj)
  {
    XStream stream = new XStream();
    // 更改List类型的元素的显示名称
    stream.alias("中国", List.class);
    stream.alias("省份", Province.class);
    stream.alias("城市", City.class);
    stream.alias("区-县", String.class);
    // 把Province类中的name属性生成为元素的属性
    stream.useAttributeFor(Province.class, "name");
    // 把JavaBean中的集合元素直接变成子节点,面不另外再生成一个节点,该节点包含所有的子节点:
    // 如:
    /*
     * <cities>
     * <city />
     * <city />
     * </cities>
     *
     * 转换成:
     * <city />
     * <city />
     */
    // 集合类型:cities为List类型
    stream.addImplicitCollection(Province.class, "cities");
    // 数组类型:counties为String[] 类型
    stream.addImplicitArray(City.class, "counties");
    return stream.toXML(obj);
  }
}

Test result:


##

<中国>
 <省份 name="广东省">
  <城市>
   <name>广州市</name>
   <description>广东省省会</description>
   <区-县>海珠区</区-县>
   <区-县>天河区</区-县>
   <区-县>白云区</区-县>
  </城市>
  <城市>
   <name>韶关</name>
   <description>粤北城市</description>
  </城市>
 </省份>
 <省份 name="北京市">
  <城市>
   <name>东城区</name>
   <description>北京市东城区</description>
  </城市>
  <城市>
   <name>西城区</name>
   <description>北京市西城区</description>
  </城市>
 </省份>
</中国>

The above is the detailed content of XML document conversion method implemented in Javabean based on xstream package. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn