解组XML并将其存储到Multimapping中

时间:2018-09-28 13:33:29

标签: java xml jaxb

这是我的xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<input1>
  <concept name="Rad order">
    <input>varReasonForexam=Infection, post-operative;varReasonID=19666904</input>
  </concept>
  <concept name="Inpatient Encounter">
    <input>varFirstName=park123a;varLastName=sssrk123a;varSex=Male</input>
  </concept>
</input1>

这是我的Concept.java

public class Concept {
    private String input;
    private String name;
    public Concept() {} 

    public String getinput() {  
        return input;  
    }
    public void setinput(String input){  
        this.input = input;  
    }

    @XmlAttribute
    public String getname() {  
        return name;  
    }
    public void setname(String name){  
        this.name = name;  
    }
}

这是我的Input1.java

import java.util.List;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Input1 {    
    private List<Concept> concept;

    @XmlElement 
    public List<Concept> getconcept() {
        return concept;
    }
    public void setconcept(List<Concept> concept) {
        this.concept = concept;
    }
}

这是主类RefactorClassArray1.java

import java.io.File;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.util.ArrayList;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import java.util.Set;

public class RefactorClassArray1 {
    public static void main(String[] args) {

    ArrayList<String> thisIsAStringArray = new ArrayList<String>();
    Multimap<String, String> strMapVariables = ArrayListMultimap.create();
    try {
        JAXBContext context = JAXBContext.newInstance(Input1.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        Input1 input = (Input1) unmarshaller.unmarshal(new File("varFile.xml"));

        List<Concept> list=input.getconcept();
        int isize = list.size();
        for(int i = 0; i < isize; i++) 
        {
            Concept concept = list.get(i);
            String strconname = concept.getname();
            String strinput= concept.getinput();
            String [] strarrinput = strinput.split(";");
            int arrsize = strarrinput.length ;
            String strinputval ;
            for (int j=0; j<arrsize; j++) {
                strinputval = strarrinput[j];
                strMapVariables.put(strconname, strinputval); 
            }
        }
        Set<String> keys = strMapVariables.keySet();
        for(String key: keys) {
            thisIsAStringArray.add(key);
        }
        System.out.println("thisIsAStringArray:" + thisIsAStringArray); 
    } catch (JAXBException e) {
        e.printStackTrace();
    }
 }
}

我得到的输出是

  

strMapVariables:{排序顺序= [varReasonForexam =感染,手术后,varReasonID = 19666904],住院患者遇到的情况[[varFirstName = park123a,varLastName = sssrk123a,varSex = Male]}   thisIsAStringArray:[乱序,住院中遇到]

我的问题是这样的:
如果我在XML的concept name="Rad order"下添加了一个输入, 这是我的新xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<input1>
  <concept name="Rad order">
    <input>varReasonForexam=Infection, post-operative;varReasonID=19666904</input>
    <input>varReasonForpain=Ipain, pelvic;varID=19666905<input>
  </concept>
  <concept name="Inpatient Encounter">
    <input>varFirstName=park123a;varLastName=sssrk123a;varSex=Male</input>
  </concept>
</input1>

预期的strMapVariables输出如下:

  

strMapVariables:{Rad order = [varReasonForexam =感染,术后,varReasonID = 19666904],Rad order = [varReasonForpain = Ipain,骨盆; varID = 19666905]住院患者遇到= [varFirstName = park123a,varLastName = sssrk123 =男]}

我只想使用JAXB和映射,邮政编码需要输出作为映射的集合。如何实现呢?

0 个答案:

没有答案