xstream解析xml中有很多重复的field的xml

前端之家收集整理的这篇文章主要介绍了xstream解析xml中有很多重复的field的xml前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

有些情况下,xml文件是这样的

<body>
  <entryList>
    <field name="forumName">General Discussion</field>
    <field name="createDate">2013-09-11 12:37:02</field>
  </entryList>
</body>

对于这种xml使用xml解析工具解析是很好处理的,但是对xml绑定工具就不能直接绑定了。需要稍作修改

如果使用xstream,就要添加一个转化器实现Converter接口。先把接口贴出来:

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 FieldConverter implements Converter {

	@Override
	public boolean canConvert(@SuppressWarnings("rawtypes") Class type) {
		if (type == Field.class) {
			return true;
		}
		return false;
	}

	@Override
	public void marshal(Object source,HierarchicalStreamWriter writer,MarshallingContext context) {
		Field field = (Field)source;
		//writer.startNode("field");
		writer.addAttribute("name",field.getName());
		writer.setValue(field.getValue());
		//writer.endNode();
	}

	@Override
	public Object unmarshal(HierarchicalStreamReader reader,UnmarshallingContext context) {
		Field field = new Field();
		String name = reader.getAttribute("name");
		field.setName(name);
		String value = reader.getValue();
		field.setValue(value);
		return field;
	}

}
下面是两个POJO封装类:
import java.util.List;

public class Body {
	List<Field> entryList;

	public Body() {
		super();
	}

	public List<Field> getEntryList() {
		return entryList;
	}

	public void setEntryList(List<Field> entryList) {
		this.entryList = entryList;
	}

	
}

下面这个Filed类用来封装里面的field域,name对应name属性值,value对应filed的值。标准的做法应该要有一个<value>标签的。
public class Field {
	String name;
	String value;
	
	public Field() {
		super();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
}

下面的代码就是main方法,用来测试的:
import java.util.ArrayList;
import java.util.List;

import com.thoughtworks.xstream.XStream;

public class TestXml {
	public static void main(String[] aa) {
		List<Field> entryList = new ArrayList<Field>();
		Field field = new Field();
		field.setName("forumName");
		field.setValue("General Discussion");
		
		entryList.add(field);
		
		Field field2 = new Field();
		field2.setName("createDate");
		field2.setValue("2013-09-11 12:37:02");
		
		entryList.add(field2);
		
		Body body = new Body();
		body.setEntryList(entryList);
		
		XStream xstream = new XStream();
		xstream.registerConverter(new FieldConverter());
		
		xstream.alias("field",Field.class);
		xstream.alias("body",Body.class);
		
		String xml = xstream.toXML(body);
		System.err.println(xml);
		
		Body body2 = (Body)xstream.fromXML(xml);
		System.out.println(body2);
	}
	
}
如果不使用FieldConverter,通过指定别名和属性输出的xml value值是带有标签的,不符合规则。使用了就会输出带field的属性和值的,而value是直接作为field的值,不会在出现<value>标签了。

猜你在找的XML相关文章