基于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类时,就会使用你指定的解码和编码器

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值