加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 05:12:53 所属栏目:百科 来源:网络整理
导读:接上篇,对于xml中含有重复field的xml解析使用xml解析工具比较简单,使用绑定工具就要稍作修改。如下xml: entryListfield name="userName"yinlei/fieldfield name="age"19/field/entryList 如果使用jibx来绑定,需要另外写一个映射器,用来出来这中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类时,就会使用你指定的解码和编码器

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读