首先做一个主要工具类,然后以xml的根元素为一个实体,实体中串联子元素的实体,子元素的属性分别代表其元素,注意在getter上会反复获取两次,需要手动get前注入@XmlElement
xml字符串样板
<?xml version="1.0" encoding="UTF-8"?> <MSG xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.travelsky.com/2006/dcsi"> <META> <SNDR>SCS_PE</SNDR> <SEQN>123</SEQN> <DTTM>20100704113058</DTTM> <TYPE>PSIN</TYPE> <STYP>PSCB</STYP> </META> <PSIN> <BID>054248001</BID> <FID>CA123</FID> <FDATE>15SEP15</FDATE> <PSCB> <PSST>O</PSST> <PSTM>20060324112858</PSTM> </PSCB> </PSIN> </MSG>
编写工具类代码
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> T xmlToBean(String xml,Class<T> 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 <T> List<T> xmlToBean(List<String> xmlList,Class<T> msgVo)throws JAXBException,SAXException,ParserConfigurationException { if (msgVo==null) { return Collections.EMPTY_LIST; } List<T> beanList = new LinkedList<T>(); 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 { /** * <pre> * 行李条号 * </pre> * */ private String bid; @XmlElement(name="BID") public String getBid() { return bid; } /** * <pre> * 行李所属航班号 * </pre> * */ private String fid; @XmlElement(name="FID") public String getFid() { return fid; } /** * <pre> * 航班日期 * </pre> * */ private String fdate; @XmlElement(name="FDATE") public String getFdate() { return fdate; } /** * <pre> * 安检消息标识(Y:开包 S:安扣) * </pre> * */ 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 { /** * <pre> * 安检状态 * </pre> * */ private String psst; @XmlElement(name="PSST") public String getPsst() { return psst; } /** * <pre> * 安检时间 * </pre> * */ private String pstm; @XmlElement(name="PSTM") public String getPstm() { return pstm; } }