简体   繁体   English

在Java中,当我将XML解析为Object时,如何注释应为我的类而不是XML的rootElement的元素

[英]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.

相关问题 如何使用 Java 和 SAX 解析我的简单 XML 文件? - How do I parse my simple XML file with Java and SAX? Simple-xml:如何注释我的“点”class - Simple-xml: how to annotate my “point” class 为什么我不能将我的已删除的HTML解析为XML? - Why can't I parse my scraped HTML into XML? 我如何使用我的 java object 创建一个 XML 文件并为此 XML 文件设置特定格式? - How could i create a XML file using my java object and set the specific format for this XML file? 如何将导出的 XML 写入 XML 文件? - How can I write my exported XML to an XML File? 如何将整数从Java文件发送到XML文件夹? (Android Studio) - How can I send a integer from my Java file to my XML folder? (Android Studio) 如何解析XML标记 <m:properties> 对于使用Java的Android应用程序? - How do I parse the XML tag <m:properties> for my Android application using Java? 如何使用模式验证我的 XML,JAXB 的 XMLStreamReader 一次只读取一个对象/元素? - How can I validate my XML using a schema, reading only one object/element at a time by JAXB's XMLStreamReader? 尝试解析XML文件时如何停止截断我的XML文件 - How to stop my XML file from being truncated when I try and parse the file 当我使用xml对象时,我的应用崩溃 - when I use xml object my app crashes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM