XStream 导入导出带属性 XML

生成和解析如下XML

<resources>
  <string name="name0">text0string>
  <string name="name1">text1string>
  <string name="name2">text2string>
  <string name="name3">text3string>
  <string name="name4">text4string>
  <string name="name5">text5string>
  <string name="name6">text6string>
  <string name="name7">text7string>
  <string name="name8">text8string>
  <string name="name9">text9string>
resources>

定义JavaBean

Resources.java

@XStreamAlias("resources")
public class Resources {
    @XStreamImplicit
    public List<Node> resources;

    public Resources(List<Node> resources) {
        this.resources = resources;
    }

    @XStreamAlias("string")
    @XStreamConverter(NodeConverter.class)
    public static class Node {
        @XStreamAsAttribute
        public String name;
        public String text;

        public Node(String name, String text) {
            this.name = name;
            this.text = text;
        }

        @Override
        public String toString() {
            final StringBuilder sb = new StringBuilder("{");
            sb.append("\"name\":\"")
                    .append(name).append('\"');
            sb.append(",\"text\":\"")
                    .append(text).append('\"');
            sb.append('}');
            return sb.toString();
        }
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("{");
        sb.append("\"resources\":")
                .append(resources);
        sb.append('}');
        return sb.toString();
    }
}

NodeConverter.java

public class NodeConverter implements Converter {

    public static final String ATTRIBUTE_NAME = "name";

    @Override
    public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
        Node node = (Node) source;
        writer.addAttribute("name", node.name);
        writer.setValue(node.text);
    }

    @Override
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
        String name = reader.getAttribute(ATTRIBUTE_NAME);
        String text = reader.getValue();
        return new Node(name, text);
    }

    @Override
    public boolean canConvert(Class type) {
        return type.equals(Node.class);
    }
}
public static void main(String args[]) throws Exception {
        List<Node> nodes = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            nodes.add(new Node("name" + i, "text" + i));
        }
        Resources resources = new Resources(nodes);

        XStream xStream = new XStream();
        xStream.processAnnotations(Resources.class);
        String xml = xStream.toXML(resources);
        System.out.println(xml);

        Resources resourcesFromXML = (Resources) xStream.fromXML(xml);
        System.out.println(resourcesFromXML);
    }

运行结果:

System.out.println(xml);
<resources>
  <string name="name0">text0string>
  <string name="name1">text1string>
  <string name="name2">text2string>
  <string name="name3">text3string>
  <string name="name4">text4string>
  <string name="name5">text5string>
  <string name="name6">text6string>
  <string name="name7">text7string>
  <string name="name8">text8string>
  <string name="name9">text9string>
resources>
System.out.println(resourcesFromXML);
{"resources":[{"name":"name0","text":"text0"}, {"name":"name1","text":"text1"}, {"name":"name2","text":"text2"}, {"name":"name3","text":"text3"}, {"name":"name4","text":"text4"}, {"name":"name5","text":"text5"}, {"name":"name6","text":"text6"}, {"name":"name7","text":"text7"}, {"name":"name8","text":"text8"}, {"name":"name9","text":"text9"}]}

你可能感兴趣的:(Android,java,XML)