[英]In Java when i parse an XML to Object how can i annotate my element which should be my Class and not my rootElement for an XML
I have an xml which is looks like this: 我有一个看起来像这样的xml:
<?xml version="1.0" encoding="UTF-8"?>
<input>
<flight flight="LX179" dep="SIN" arr="ZRH" aircraftRegistration="HBJMN"
paxWeight="10000" />
</input>
And i used jaxb to unmarshall it to an object 我用jaxb将其解组到一个对象
So my class looks like this , if i delete the input tag from the xml, and make the Public Class Flight to my @RootElement, then it works fine, but thats not good for me, i need them there... pls could anyone help with this ? 所以我的班级是这样的,如果我从xml中删除了输入标签,然后将Public Class Flight转到了我的@RootElement,则可以正常工作,但是那对我不好,我在那里需要它们……请任何人帮助这个吗?
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "input")
@XmlElement //I GOT THE ERROR HERE
public class Flight {
private String flight,dep,arr,aircraftRegistration;
private int paxWeight;
public Flight() {
}
public Flight(String flight, String dep, String arr, String aircraftRegistration,
int paxWeight, ) {
this.flight = flight;
this.dep = dep;
this.arr = arr;
this.aircraftRegistration = aircraftRegistration;
this.paxWeight = paxWeight;
public String getFlight() {
return flight;
}
@XmlAttribute
public void setFlight(String flight) {
this.flight = flight;
}
public String getDep() {
return dep;
}
@XmlAttribute
public void setDep(String dep) {
this.dep = dep;
}
public String getArr() {
return arr;
}
@XmlAttribute
public void setArr(String arr) {
this.arr = arr;
}
public String getAircraftRegistration() {
return aircraftRegistration;
}
@XmlAttribute
public void setAircraftRegistration(String aircraftRegistration) {
this.aircraftRegistration = aircraftRegistration;
}
public int getPaxWeight() {
return paxWeight;
}
@XmlAttribute
public void setPaxWeight(int paxWeight) {
this.paxWeight = paxWeight;
}
My parsing method 我的解析方法
private void beolvasas() {
try {
File file = new File("/input.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Input.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Input flight = (Input) jaxbUnmarshaller.unmarshal(file);
flightList.add(flight);
} catch (JAXBException ex) {
ex.printStackTrace();
}
}
private void kiir() {
for (Input input : flightList) {
System.out.println(input);
}
}
My Input class looks like this: 我的Input类看起来像这样:
@XmlRootElement
public class Input {
private Flight flight;
@XmlRootElement
public class Input {
private List<Flight> lista;
public Input() {
}
public Input(List<Flight> list) {
this.lista = lista;
}
public List<Flight> getList() {
return lista;
}
@XmlElement
public void setList(List<Flight> list) {
this.lista = list;
}
This is the last xml File... 这是最后一个xml文件...
<?xml version="1.0" encoding="UTF-8"?>
<masterdata>
<airlines>
<airline id="1" name="Lufthansa" code="LH" minUnderload="1000"/>
<airline id="2" name="Swiss" code="LX" minUnderload="500" />
</airlines>
<aircraftTypes>
<aircraftType id="380" name="Airbus 380-800" maxZFW="361000" maxLAW="386000" maxTOW="560000" maxTXW="562000" maxFuel="256000"/> <aircraftType id="747" name="Boeing 747-8" maxZFW="295289" maxLAW="312072" maxTOW="447696" maxTXW="449056" maxFuel="193280"/>
<aircraftType id="340" name="Airbus 340-600" maxZFW="245000" maxLAW="259000" maxTOW="368000" maxTXW="369200" maxFuel="163600"/>
</aircraftTypes>
<aircrafts>
<aircraft id="11" ownerAirlineId="1" registration="DAIML" typeId="380" startw="276800" />
<aircraft id="12" ownerAirlineId="1" registration="DABVT" typeId="747" startw="162400" />
<aircraft id="13" ownerAirlineId="1" registration="DAIHQ" typeId="340" startw="177020" />
<aircraft id="21" ownerAirlineId="2" registration="HBJMN" typeId="340" startw="176445" />
</aircrafts>
You theoretically have this 理论上你有这个
<input>
<flight flight="LX179" dep="SIN" arr="ZRH" aircraftRegistration="HBJMN"
paxWeight="10000" />
<flight flight="LX178" dep="SIN" arr="YUL" aircraftRegistration="ABCDE"
paxWeight="10000" />
</input>
Therefore you have a collection of <flight>
elements within one <input>
element. 因此,在一个
<input>
元素中有一个<flight>
元素的集合。 You have to model that with Java. 您必须使用Java进行建模。
public class Input {
public List<Flight> lista;
}
public class Flight {}
Once you have that, you have to add the appropriate annotations. 一旦有了它,就必须添加适当的注释。
<input>
is the root element, so Input
should be a root element. <input>
是根元素,因此Input
应该是根元素。
@XmlRootElement(name = "input")
public class Input {
It has a collection of elements called <flight>
, so that field should be annotated 它具有称为
<flight>
的元素的集合,因此应对字段进行注释
@XmlElement(name = "flight")
public List<Flight> lista;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.