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

你可能感兴趣的:(java,xml,jibx)