jaxb 解析多列数据的xml 映射成list

  
  
package com.*.getMap;  
  
import javax.xml.bind.annotation.*;  
import java.util.ArrayList;  
import java.util.List;  
  
//添加注解@XmlAccessorType(XmlAccessType.FIELD)可以把@xmlElement 的注解写在类的属性上。
//如果xml属性与映射对象的属性不一致也需添加此注解。把映射对象中注解name的值改成xml中我们需要对应的属性名称。  
  
@SuppressWarnings("all")  
@XmlAccessorType(XmlAccessType.FIELD)  
@XmlRootElement(name = "rfics")  
public class Rfics {   
  
    @XmlAnyElement(lax = true)   
    private List items;   
  
    public Rfics() {  
         items = new ArrayList();  
    }   
  
    public List getItems() {  
        return items;   
    }  
      
    public void setItems(List items) {  
        this.items = items;   
    }  
  
} 


package com.*.getMap;

import javax.xml.bind.annotation.*;

@SuppressWarnings("all")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="rfisc")
public class RfiscDo { 

    @XmlAttribute(name = "code") 
    private String rfisc; 
    @XmlAttribute(name = "desc") 
    private String description; 

    public String getRfisc() { 
        return rfisc; 
    } 

    public void setRfisc(String rfisc) { 
        this.rfisc = rfisc; 
    } 

    public String getDescription() { 
        return description; 
    } 

    public void setDescription(String description) { 
        this.description = description; 
    }
} 

  
  
  
package com.*.getMap;  
  
import org.springframework.core.io.ClassPathResource;  
import org.springframework.core.io.Resource;  
  
import javax.xml.bind.JAXBContext;  
import javax.xml.bind.JAXBException;  
import javax.xml.bind.Unmarshaller;  
import javax.xml.transform.stream.StreamSource;  
import java.io.File;  
import java.io.IOException;  
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;  
  
/**  
 * 获得xml属性的map方法  
 */  
@SuppressWarnings("all")  
public class GetMapUtil {  
  
    private static Resource resource;  
    private static File file;  
    private static StreamSource streamSource;  
  
    static {  
        try {  
            resource = new ClassPathResource("rfic.xml");  
            file = resource.getFile();  
            streamSource = new StreamSource(file);  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
  
    /**  
     *解析xml内容返回list  
     */  
    private static  List parseXmlToList(Class listClass , ClassbeanClass) throws Exception {  
        JAXBContext context = JAXBContext.newInstance(listClass , beanClass);  
        Unmarshaller unmarshaller = context.createUnmarshaller();  
        List list = xmlToObject(unmarshaller, listClass);  
        return list;  
    }  
    private static  List xmlToObject(Unmarshaller unmarshaller, Class listClass) throws JAXBException, IOException {  
        Rfics rfics = (Rfics) unmarshaller.unmarshal(streamSource , listClass).getValue();  
        return rfics.getItems();  
    }  
  
    /**  
     * @return map  
     */  
    public static Map getMap(){  
        Map map = null;  
        List list = null;  
        try {  
            map = new HashMap();  
            list = parseXmlToList(Rfics.class , RfiscDo.class);  
            for (RfiscDo rfiscDo : list) {  
                map.put(rfiscDo.getRfisc() , rfiscDo.getDescription());  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return map;  
    }  
} 

<测试类>  
  
    public static void main(String[] args) {  
        System.out.println(GetMapUtil.getMap().get("1329"));  
    }  

你可能感兴趣的:(原创)