微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

基于jibx解析xml中有很多field的xml

接上篇,对于xml中含有重复field的xml解析使用xml解析工具比较简单,使用绑定工具就要稍作修改。如下xml:

<entryList>
<field name="userName">yinlei</field>
<field name="age">19</field>
</entryList>

如果使用jibx来绑定,需要另外写一个映射器,用来出来这中xml.

使用下面这个POJO来映射上述xml

public class Field {
	private String name;
	private 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;
	}
}
jibx的绑定映射文件这样来写,指定具体的marshaller和unmarshaller:
    <collection field="entryList" name="entryList" usage="optional" create-type="java.util.ArrayList"><!-- name属性就是xml的外层属性 -->
      <structure type="com.hch.testjibx.Field" name="field" marshaller="com.hch.testjibx.FieldMapper" unmarshaller="com.hch.testjibx.FieldMapper">
      </structure>
    </collection>

marshaller和unmarshaller的值就是你要实现的映射类:
import org.jibx.runtime.IAliasable;
import org.jibx.runtime.IMarshaller;
import org.jibx.runtime.IMarshallingContext;
import org.jibx.runtime.IUnmarshaller;
import org.jibx.runtime.IUnmarshallingContext;
import org.jibx.runtime.jibXException;
import org.jibx.runtime.impl.MarshallingContext;
import org.jibx.runtime.impl.UnmarshallingContext;

public class FieldMapper implements IMarshaller,IUnmarshaller,IAliasable {
	private String uri;
	private int index;
	private String fieldName;
	
	public FieldMapper() {
		super();
		this.uri = null;
		this.index = 0;
		this.fieldName = "field";
	}

	public FieldMapper(String uri,int index,String fieldName) {
		super();
		this.uri = uri;
		this.index = index;
		this.fieldName = fieldName;
	}

	@Override
	public boolean isPresent(IUnmarshallingContext context) throws jibXException {
		return context.isAt(uri,fieldName);
	}

	@Override
	public Object unmarshal(Object object,IUnmarshallingContext context)
			throws jibXException {
		Field field = (Field) object;
		if (field == null) {
			field = new Field();
		}
		UnmarshallingContext ctx = (UnmarshallingContext) context;
		//ctx.getText();//要解析的这段xml字符串
		String name = ctx.attributeText(uri,getAttrName());//按属性名取属性值
		//ctx.getAttributeValue(0);//按照顺序取属性值
		//ctx.getAttributeCount();//属性数量
		field.setName(name);
		
		ctx.parsePastStartTag(uri,fieldName);//开始解析
		String value = ctx.parseContentText();
		//ctx.getText();
		//ctx.getName();
		ctx.parsePastEndTag(uri,fieldName);//解析后要关闭
		field.setValue(value);
		return field;
	}

	@Override
	public boolean isExtension(String arg0) {
		return false;
	}

	@Override
	public void marshal(Object source,IMarshallingContext context)
			throws jibXException {
		Field field = (Field) source;
		MarshallingContext ctx = (MarshallingContext) context;
		ctx.startTagAttributes(index,fieldName);
		ctx.attribute(index,getAttrName(),field.getName()).closeStartContent();
		ctx.content(field.getValue());
		ctx.endTag(index,fieldName);
	}

	public String getAttrName() {
		return "name";
	}
	
}
这样,jibx在遇到Field类时,就会使用你指定的解码和编码器

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。