利用jaxb解析xml字符串转对象,忽略xml的命名空间

首先做一个主要工具类,然后以xml的根元素为一个实体,实体中串联子元素的实体,子元素的属性分别代表其元素,注意在getter上会反复获取两次,需要手动get前注入@XmlElement

xml字符串样板


	
	
	SCS_PE	
	123	
	20100704113058	
	PSIN	
	PSCB	
	
	
	054248001
CA123
	15SEP15
		
		O
		20060324112858
		

 

 

编写工具类代码

package com.royalnu.common.utils.xml;
import java.io.StringReader;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.Source;
import javax.xml.transform.sax.SAXSource;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import com.royalnu.common.exception.CommonException;

public class XmlUtils {
	
	/**
	 * 一个xml字符串转对象,忽略xml命名空间
	 * @param xml
	 * @param msgVo 根元素实体
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static  T xmlToBean(String xml,Class msgVo)throws JAXBException,SAXException,ParserConfigurationException,CommonException {
		if (msgVo==null) {
		   return null;
		}		
		JAXBContext context = JAXBContext.newInstance(msgVo);		 
        Unmarshaller unmarshaller = context.createUnmarshaller();	
        Source source = trunSource(xml);    
		return (T)unmarshaller.unmarshal(source);	
	}
	
    /**
     * xml字符串集合转对象,忽略xml命名空间
     * @param xml xml字符串集合
     * @param msgVo 根元素实体
     */
	@SuppressWarnings("unchecked")
	public static   List xmlToBean(List xmlList,Class msgVo)throws JAXBException,SAXException,ParserConfigurationException {
		if (msgVo==null) {
			return Collections.EMPTY_LIST;
		}		
		List beanList = new LinkedList();
		JAXBContext context = JAXBContext.newInstance(msgVo);		 
        Unmarshaller unmarshaller = context.createUnmarshaller();
        for (String xmlStr : xmlList) {
             Source source = trunSource(xmlStr);
             beanList.add((T)unmarshaller.unmarshal(source));     		
		}
        return  beanList;
	}
	
	private static Source trunSource(String xmlStr) throws SAXException,ParserConfigurationException {
		 StringReader reader = new StringReader(xmlStr); 
	     SAXParserFactory sax = SAXParserFactory.newInstance();  
         sax.setNamespaceAware(false);  
         XMLReader xmlReader = sax.newSAXParser().getXMLReader();
         Source source = new SAXSource(xmlReader, new InputSource(reader));  
		 return source;		
	}
	
}

 

对应实体

//根元素实体
package com.royalnu.psis.interfaces.psp.api.vo;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import lombok.Setter;
@Setter
@XmlAccessorType(XmlAccessType.PROPERTY)  
@XmlRootElement(name = "MSG") 
public class PspLuggageSeizeInfoMsgVo {

	private PspLuggageSeizeInfoVo pspLuggageSeizeInfoVo;
	@XmlElement(name="PSIN")
    public PspLuggageSeizeInfoVo getPspLuggageSeizeInfoVo() {
		return pspLuggageSeizeInfoVo;
	}
}


//第二个串联的实体
package com.royalnu.psis.interfaces.psp.api.vo;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;

import lombok.Setter;

@Setter
@XmlAccessorType(XmlAccessType.PROPERTY)  
public class PspLuggageSeizeInfoVo {

	 /**
     * 
     * 行李条号
     * 
* */ private String bid; @XmlElement(name="BID") public String getBid() { return bid; } /** *
     * 行李所属航班号
     * 
* */ private String fid; @XmlElement(name="FID") public String getFid() { return fid; } /** *
     * 航班日期
     * 
* */ private String fdate; @XmlElement(name="FDATE") public String getFdate() { return fdate; } /** *
     * 安检消息标识(Y:开包 S:安扣)
     * 
* */ private PspLuggageSeizeInfoPscbVo pscbVo; @XmlElement(name="PSCB") public PspLuggageSeizeInfoPscbVo getPscbVo() { return pscbVo; } private String uuid; @XmlElement(name="UUID") public String getUuid() { return uuid; } } //最后一个串联的实体 package com.royalnu.psis.interfaces.psp.api.vo; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import lombok.Setter; @Setter @XmlAccessorType(XmlAccessType.PROPERTY) public class PspLuggageSeizeInfoPscbVo { /** *
     * 安检状态
     * 
* */ private String psst; @XmlElement(name="PSST") public String getPsst() { return psst; } /** *
     * 安检时间
     * 
* */ private String pstm; @XmlElement(name="PSTM") public String getPstm() { return pstm; } }

 

 

你可能感兴趣的:(java)