整理 XML,并将一些新数据传输到 JAVA 中的新 XML

umarshalling the XML, and transferring some new data to a new XML in JAVA

我有一个格式示例 XML,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<Institutions>
    <Institution type = "School">
        <place>Munich</place>
        <Subjects>
            <Subject>English</Subject>
            <Subject>Deutsch</Subject>
        </Subjects> 
    </Institution>  
    <Institution type ="College">
        <place>Cologne</place>
        <Subjects>
            <Subject>Sports</Subject>
            <Subject>Gym</Subject>
        </Subjects> 
    </Institution>
    <Institution type= "University">
        <place>Hamburg</place>
        <Subjects>
            <Subject>MElectrical</Subject>
            <Subject>MComputers</Subject>
        </Subjects>
    </Institution>
</Institutions>

我也有类机构和机构

Institutions.java

import lombok.Data;
@XmlRootElement(name="Institutions")
@XmlAccessorType(XmlAccessType.FIELD)
@Data
public class Institutions {
    public List<Institution> Institution;   
}

Institution.java

@Data
@XmlAccessorType(XmlAccessType.FIELD)
public class Institution {

    @XmlAttribute(name = "type")
    private String type;
    
    @XmlElement(name = "place")
    private String place;
    
    @XmlElementWrapper(name="Subjects")   
    @XmlElement(name="Subject") 
    private List<String> subjects;
}

现在我有了一个主解析器,它负责解组和编组。我想向其中添加更多数据,但是这次,我只想要新生成的 XML.

中的新添加数据

例如:我在主程序中创建了一个逻辑

public static void main(String[] args) {
        // TODO Auto-generated method stub
        List<Institutions> entries = new ArrayList<Institutions>();
    
        
        try {
            
            File xmlFile = new File("sample.xml");
            JAXBContext jaxbContext;
            jaxbContext = JAXBContext.newInstance(Institutions.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            Institutions xmlentries = (Institutions) jaxbUnmarshaller.unmarshal(xmlFile);
            entries.add(xmlentries);
            List<Institution> institutionList = xmlentries.getInstitution();
            Institution newInstitution = null;
            for(Institution i : institutionList) {
                if(i.getType().equals("School")) {
                    newInstitution = new Institution();
                    newInstitution.setPlace("Augsburg"); //(1)
                    newInstitution.setType("School");
                    List<String> subjectList = new ArrayList<String>();
                    subjectList.add("Math");
                    subjectList.add("Science");
                    newInstitution.setSubjects(subjectList);
                    break;
                }               
            }
            if(newInstitution!=null) {
                institutionList.add(newInstitution);
            }
Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(xmlentries, new File("outputsample.xml"));//(2)
            
        }catch (JAXBException e) {
            e.printStackTrace();
        } catch (FactoryConfigurationError e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }

这里我根据学校的位置添加一个新条目。

(1) 是否可以根据给定的条件将此处的输入字段设为通用/。我不想再次输入学校名称。 (2) 生成 XML,但如何确保我只有最新添加的内容?

当前的 outputsample.xml 看起来像这样:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Institutions>
    <Institution type="School">
        <place>Munich</place>
        <Subjects>
            <Subject>English</Subject>
            <Subject>Deutsch</Subject>
        </Subjects>
    </Institution>
    <Institution type="College">
        <place>Cologne</place>
        <Subjects>
            <Subject>Sports</Subject>
            <Subject>Gym</Subject>
        </Subjects>
    </Institution>
    <Institution type="University">
        <place>Hamburg</place>
        <Subjects>
            <Subject>MElectrical</Subject>
            <Subject>MComputers</Subject>
        </Subjects>
    </Institution>
    <Institution>
        <place>Augsburg</place>
        <Subjects>
            <Subject>Math</Subject>
            <Subject>Science</Subject>
        </Subjects>
    </Institution>
</Institutions>

但是,我希望 outputsample.xml 看起来像这样(所有添加的 mdificaaitons

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Institutions>
    <Institution>
        <place>Augsburg</place>
        <Subjects>
            <Subject>Math</Subject>
            <Subject>Science</Subject>
        </Subjects>
    </Institution>
</Institutions>

这里有什么指导吗?这是如何实现的? aftermarshall 也不能在主程序中应用?我需要删除前面提到的内容并保留最近添加的内容

这将产生您正在寻找的输出:

public class Main {
    public static void main(String[] args) throws JAXBException, XMLStreamException {
        final InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("sample.xml");
        final XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
        final Unmarshaller unmarshaller = JAXBContext.newInstance(Root.class).createUnmarshaller();
        final Root root = unmarshaller.unmarshal(xmlStreamReader, Root.class).getValue();
        System.out.println(root.toString());

        Marshaller marshaller = JAXBContext.newInstance(Root.class).createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(root, System.out);

        System.out.println();
        System.out.println(" ************************************** ");
        System.out.println();
        
        Institution newInstitution = new Institution();
        List<Institution> institutionList = root.getInstitution();
        for(Institution i : institutionList) {
            if(i.getType().equals("School")) {
                newInstitution = new Institution();
                newInstitution.setPlace("Augsburg"); //(1)
                newInstitution.setType("School");
                List<String> subjectList = new ArrayList<String>();
                subjectList.add("Math");
                subjectList.add("Science");
                newInstitution.setSubjects(subjectList);
                break;
            }
        }

        Marshaller newMarshaller = JAXBContext.newInstance(Institution.class).createMarshaller();
        newMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        QName qName = new QName("Institution");
        JAXBElement<Institution> newRoot = new JAXBElement<Institution>(qName, Institution.class, newInstitution);
        newMarshaller.marshal(newRoot, System.out);
    }
}

输出:

Root(Institution=[Institution(type=School, place=Munich, subjects=[English, Deutsch]), Institution(type=College, place=Cologne, subjects=[Sports, Gym]), Institution(type=University, place=Hamburg, subjects=[MElectrical, MComputers])])
<Institutions>
   <Institution type="School">
      <place>Munich</place>
      <Subjects>
         <Subject>English</Subject>
         <Subject>Deutsch</Subject>
      </Subjects>
   </Institution>
   <Institution type="College">
      <place>Cologne</place>
      <Subjects>
         <Subject>Sports</Subject>
         <Subject>Gym</Subject>
      </Subjects>
   </Institution>
   <Institution type="University">
      <place>Hamburg</place>
      <Subjects>
         <Subject>MElectrical</Subject>
         <Subject>MComputers</Subject>
      </Subjects>
   </Institution>
</Institutions>
 ************************************** 

<?xml version="1.0" encoding="UTF-8"?>
<Institution type="School">
   <place>Augsburg</place>
   <Subjects>
      <Subject>Math</Subject>
      <Subject>Science</Subject>
   </Subjects>
</Institution>

使用 afterUnmarshal:

你的根class

@XmlRootElement(name = "Institutions")
@XmlAccessorType(XmlAccessType.FIELD)
@Data
public class Root {
    public List<Institution> Institution;

    private void afterUnmarshal(Unmarshaller unmarshaller, Object parent) throws JAXBException {
        System.out.println("AFTER UNMARSHAL");
        Institution newInstitution = new Institution();
        List<Institution> institutionList = Institution;
        for (Institution i : institutionList) {
            if (i.getType().equals("School")) {
                newInstitution = new Institution();
                newInstitution.setPlace("Augsburg"); //(1)
                newInstitution.setType("School");
                List<String> subjectList = new ArrayList<String>();
                subjectList.add("Math");
                subjectList.add("Science");
                newInstitution.setSubjects(subjectList);
                break;
            }
        }

        Marshaller newMarshaller = JAXBContext.newInstance(Institution.class).createMarshaller();
        newMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        QName qName = new QName("Institution");
        JAXBElement<Institution> newRoot = new JAXBElement<Institution>(qName, Institution.class, newInstitution);
        newMarshaller.marshal(newRoot, System.out);
    }
}

主要:

public class Main {
    public static void main(String[] args) throws JAXBException, XMLStreamException {
        final InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("sample.xml");
        final XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
        final Unmarshaller unmarshaller = JAXBContext.newInstance(Root.class).createUnmarshaller();
        final Root root = unmarshaller.unmarshal(xmlStreamReader, Root.class).getValue();
        System.out.println(root.toString());

        Marshaller marshaller = JAXBContext.newInstance(Root.class).createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(root, System.out);
    }
}

输出:

AFTER UNMARSHAL
<?xml version="1.0" encoding="UTF-8"?>
<Institution type="School">
   <place>Augsburg</place>
   <Subjects>
      <Subject>Math</Subject>
      <Subject>Science</Subject>
   </Subjects>
</Institution>
Root(Institution=[Institution(type=School, place=Munich, subjects=[English, Deutsch]), Institution(type=College, place=Cologne, subjects=[Sports, Gym]), Institution(type=University, place=Hamburg, subjects=[MElectrical, MComputers])])
<Institutions>
   <Institution type="School">
      <place>Munich</place>
      <Subjects>
         <Subject>English</Subject>
         <Subject>Deutsch</Subject>
      </Subjects>
   </Institution>
   <Institution type="College">
      <place>Cologne</place>
      <Subjects>
         <Subject>Sports</Subject>
         <Subject>Gym</Subject>
      </Subjects>
   </Institution>
   <Institution type="University">
      <place>Hamburg</place>
      <Subjects>
         <Subject>MElectrical</Subject>
         <Subject>MComputers</Subject>
      </Subjects>
   </Institution>
</Institutions>

如果要添加到同一个列表: 根:

@XmlRootElement(name = "Institutions")
@XmlAccessorType(XmlAccessType.FIELD)
@Data
public class Root {
    public List<Institution> Institution;

    private void afterUnmarshal(Unmarshaller unmarshaller, Object parent) throws JAXBException {
        Institution newInstitution = new Institution();
        newInstitution.setType("School");
        newInstitution.setPlace("Augsburg");
        newInstitution.setType("School");
        List<String> subjectList = new ArrayList<String>();
        subjectList.add("Math");
        subjectList.add("Science");
        newInstitution.setSubjects(subjectList);
    }
}

输出:

Root(Institution=[Institution(type=School, place=Munich, subjects=[English, Deutsch]), Institution(type=College, place=Cologne, subjects=[Sports, Gym]), Institution(type=University, place=Hamburg, subjects=[MElectrical, MComputers])])
<Institutions>
   <Institution type="School">
      <place>Munich</place>
      <Subjects>
         <Subject>English</Subject>
         <Subject>Deutsch</Subject>
      </Subjects>
   </Institution>
   <Institution type="College">
      <place>Cologne</place>
      <Subjects>
         <Subject>Sports</Subject>
         <Subject>Gym</Subject>
      </Subjects>
   </Institution>
   <Institution type="University">
      <place>Hamburg</place>
      <Subjects>
         <Subject>MElectrical</Subject>
         <Subject>MComputers</Subject>
      </Subjects>
   </Institution>
</Institutions>