解组返回空值

时间:2017-09-29 14:52:09

标签: java xml jaxb

我正在尝试解组cars.xml,但它返回了一个空值。 当我尝试使用xml上的一辆车时,这种方法很有效,但是我遇到了List的问题。

主要课程:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

public class Teste {

  private final String USER_AGENT = "Mozilla/5.0";

  public static void main(String[] args) throws Exception {

    Teste http = new Teste();

    System.out.println("Testing 1 - Send Http GET request");
    http.sendGet();

//      System.out.println("\nTesting 2 - Send Http POST request");
//      http.sendPost();
  }

  // HTTP GET request
  private void sendGet() throws Exception {

    String url = "http://www.teste.com/cars.xml";

    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    // optional default is GET
    con.setRequestMethod("GET");

    //add request header
    con.setRequestProperty("User-Agent", USER_AGENT);

    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'GET' request to URL : " + url);
    System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));

    try {

      //Transforma xml em objeto
      JAXBContext aXBContext = JAXBContext.newInstance(Cars.class);
      Unmarshaller cu = aXBContext.createUnmarshaller();
      Cars cars = (Cars) cu.unmarshal(in);



      if(cars != null){
        System.out.println("Não é nulo!");
      }

    } catch (Exception e) {
      System.out.println(e.getMessage());
    }

  }
}

汽车类:

import java.util.ArrayList;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement (name = "Cars")

public class Cars {
  private ArrayList<Car> cars = new ArrayList<>();

  public ArrayList<Car> getCars() {
    return cars;
  }

  @XmlElement
  public void setCars(ArrayList<Car> cars) {
    this.cars = cars;
  }

}

汽车类:

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

@XmlRootElement(name = "Car")
public class Car {

  private String manufacturerCar;
  private String modelCar;
  private String doorsCar;
  private String gearshift;

  public String getManufacturerCar() {
    return manufacturerCar;
  }

  @XmlElement
  public void setManufacturerCar(String manufacturerCar) {
    this.manufacturerCar = manufacturerCar;
  }

  public String getModelCar() {
    return modelCar;
  }

  @XmlElement
  public void setModelCar(String modelCar) {
    this.modelCar = modelCar;
  }

  public String getDoorsCar() {
    return doorsCar;
  }

  @XmlElement
  public void setDoorsCar(String doorsCar) {
    this.doorsCar = doorsCar;
  }

  public String getGearshift() {
    return gearshift;
  }

  @XmlElement
  public void setGearshift(String gearshift) {
    this.gearshift = gearshift;
  }

  @Override
  public String toString() {
    return "Car{" + "manufacturerCar=" + manufacturerCar + ", modelCar=" + modelCar + ", doorsCar=" + doorsCar + ", gearshift=" + gearshift + '}';
  }
}

和XML:

<Cars>
<Car>
<manufacturerCar>VW</manufacturerCar>
<modelCar>FUSCA</modelCar>
<doorsCar>2</doorsCar>
<gearshift>MANUAL</gearshift>
</Car>
<Car>
<manufacturerCar>FORD</manufacturerCar>
<modelCar>FOCUS</modelCar>
<doorsCar>4</doorsCar>
<gearshift>AUTOMATICO</gearshift>
</Car>
<Car>
<manufacturerCar>FIAT</manufacturerCar>
<modelCar>UNO</modelCar>
<doorsCar>4</doorsCar>
<gearshift>MANUAL</gearshift>
</Car>
<Car>
<manufacturerCar>VW</manufacturerCar>
<modelCar>UP!</modelCar>
<doorsCar>4</doorsCar>
<gearshift>AUTOMATICO</gearshift>
</Car>
<Car>
<manufacturerCar>PORSHE</manufacturerCar>
<modelCar>Q100</modelCar>
<doorsCar>2</doorsCar>
<gearshift>AUTOMATICO</gearshift>
</Car>
<Car>
<manufacturerCar>RENAULT</manufacturerCar>
<modelCar>DUSTER</modelCar>
<doorsCar>4</doorsCar>
<gearshift>Manual</gearshift>
</Car>
<Car>
<manufacturerCar>HONDA</manufacturerCar>
<modelCar>CIVIC</modelCar>
<doorsCar>4</doorsCar>
<gearshift>Manual</gearshift>
</Car>
<Car>
<manufacturerCar>MITSUBISHI</manufacturerCar>
<modelCar>PAJERO</modelCar>
<doorsCar>4</doorsCar>
<gearshift>Manual</gearshift>
</Car>
</Cars>

当我删除项目时,它返回null :(。我认为问题是JAXB标签,但我真的不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:0)