对 XML 进行编组,并将一些新数据传输到 JAVA 中的新 XML [英] umarshalling the XML, and transferring some new data to a new XML in JAVA
问题描述
我有一个格式的示例 XML,如下所示:
I have a sample XML of the format as shown below:
<?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>
我也有针对机构和机构的课程
I have classes for Institutions and Institution as well
Institutions.java
Institutions.java
import lombok.Data;
@XmlRootElement(name="Institutions")
@XmlAccessorType(XmlAccessType.FIELD)
@Data
public class Institutions {
public List<Institution> Institution;
}
Institution.java
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 中新添加的数据.
Now i have a main parser, whch takes care of the unmarshalling and marshalling. I want to add some more data to it, however this time, I want only the new added data in the newly produced XML.
例如:我在主目录中创建了一个逻辑
For eg: I create a logic in the main
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();
}
}
这里我根据学校的位置添加了一个新条目.
Here I add a new entry based on the location of the school.
(1) 可以根据给定的条件使此处的输入字段通用/通用.我不想再次输入学校名称.(2) 生成 XML,但如何确保我只有最新添加的内容?
(1) can the entry field here be made generic/ based on te condition given. I do not want to type the school name again. (2) produces the XML, but how do I make sure that I have only the latest addition?
当前的 outputsample.xml 如下所示:
The current outputsample.xml looks like this:
<?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
But, i want the outputsample.xml to look like this ( all the added 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
吗?我需要删除前面提到的内容,保留最近添加的内容
Any guidnce here ? How could this be achieved? Also the aftermarshall
cannot be applied within the main program? I need to eliminate the previously mentioned contents and keep the recently added ones
推荐答案
这将产生您正在寻找的输出:
This would produce the output that you are looking for:
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>
这篇关于对 XML 进行编组,并将一些新数据传输到 JAVA 中的新 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!