2013-04-14 157 views
3

我正在使用JAXB解析包含以下氣象站信息的XML文件:名稱,工作站ID,緯度/經度和網址。現在我打算通過僅打印一個氣象站來測試一切。我調試了我的代碼,但是當我嘗試運行時:出現以下錯誤:JAXB XML解析的問題

線程「main」異常javax.xml.bind.UnmarshalException:意外元素(uri:「」,local:「wx_station_index」) 。預期要素是(無)

有沒有人有解釋?任何幫助,將不勝感激。我的代碼和XML的樣本被張貼如下:

//create criteria for weather station info 
@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD) 
public class StationTest { 

/** 
* @param args 
*/ 

@XmlElement(name = "station_id") 
String station_id; 

@XmlElement(name = "state") 
String state; 

@XmlElement(name = "station_name") 
String stationName; 

@XmlElement(name = "latitude") 
double latitude; 

@XmlElement(name = "longitude") 
double longitude; 

@XmlElement(name = "html_url") 
String htmlUrl; 

public String getStationID(){ 
    return station_id; 
} 

public void setStationId(String station_id) 
{ 
    this.station_id = station_id; 
} 

public String getState(){ 
    return state; 
} 

public void setState(String state) 
{ 
    this.state = state; 
} 

public String getStationName(){ 
    return stationName; 
} 

public void setStationName(String station_name) 
{ 
    this.stationName = stationName; 
} 

public double getLatitude(){ 
    return latitude; 
} 

public void setLatitude(double latitude) 
{ 
    this.latitude = latitude; 
} 

public double getLongitude(){ 
    return longitude; 
} 

public void setLongitude(double longitude) 
{ 
    this.longitude = longitude; 
} 

public String getUrl(){ 
    return htmlUrl; 
} 

public void setUrl(String url){ 
    this.htmlUrl = htmlUrl; 

} 

} 進口的java.util.ArrayList;

import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlSeeAlso; 
import javax.xml.bind.annotation.XmlAccessType; 

//create list for all weather stations 

@XmlRootElement 
@XmlSeeAlso(value = StationTest.class) 
@XmlAccessorType(XmlAccessType.FIELD) 
public class StationListTest { 

/** 
* @param args 
*/ 
@XmlElement(name = "station") 
private ArrayList<StationTest> stationList; 

public void setStationListTest(ArrayList<StationTest> StationList){ 
    this.stationList = StationList; 
} 

public ArrayList<StationTest> getStationListTest(){ 
    return stationList; 
} 

} 

import java.util.ArrayList; 

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.JAXBException; 
import javax.xml.bind.Unmarshaller; 

import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.util.ArrayList; 

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.JAXBException; 
import javax.xml.bind.Unmarshaller; 

//final Test of JAXB 
public class Test3 { 

/** 
* @param args 
*/ 
//read xml 
private static final String STATION_XML = "src/flwxindex3.xml"; 

//main method 
public static void main(String[] args) throws InterruptedException, JAXBException, FileNotFoundException { 

    //create list object 
    ArrayList<StationTest> StationList = new ArrayList<StationTest>(); 

    final JAXBContext jaxbc = JAXBContext.newInstance(StationListTest.class); 
    final Unmarshaller unmarshaller = jaxbc.createUnmarshaller(); 

    //create list for iteration, then iterate printing by name only 
    StationListTest stationListTest = (StationListTest) unmarshaller.unmarshal(new FileReader(STATION_XML)); 
    ArrayList<StationTest> list = stationListTest.getStationListTest(); 
    //final StationListTest stations = (StationListTest) unmarshaller.unmarshal(Thread.currentThread().getContextClassLoader().getResource("src/flwxindex3.xml")); 
    for (final StationTest station : list) { 
     System.out.println(station.getStationName()); 
    } 
} 

}

實際的XML的要求(思想我把它,不好意思):

<?xml version="1.0" encoding="UTF-8"?> 
<wx_station_index> 
    <credit>NOAA's National Weather Service</credit> 
    <credit_URL>http://weather.gov/</credit_URL> 
    <image> 
      <url>http://weather.gov/images/xml_logo.gif</url> 
      <title>NOAA's National Weather Service</title> 
      <link>http://weather.gov</link> 
    </image> 
    <suggested_pickup>08:00 EST</suggested_pickup> 
    <suggested_pickup_period>1140</suggested_pickup_period> 

<station> 
    <station_id>NFNA</station_id> 
    <state>FJ</state> 
    <station_name>Nausori</station_name> 
    <latitude>-18.05</latitude> 
    <longitude>178.567</longitude> 
    <html_url>http://weather.noaa.gov/weather/current/NFNA.html</html_url> 
    <rss_url>http://weather.gov/xml/current_obs/NFNA.rss</rss_url> 
    <xml_url>http://weather.gov/xml/current_obs/NFNA.xml</xml_url> 
</station> 

<station> 
    <station_id>KCEW</station_id> 
    <state>FL</state> 
      <station_name>Crestview, Sikes Airport</station_name> 
    <latitude>30.79</latitude> 
    <longitude>-86.52</longitude> 
      <html_url>http://weather.noaa.gov/weather/current/KCEW.html</html_url> 
      <rss_url>http://weather.gov/xml/current_obs/KCEW.rss</rss_url> 
      <xml_url>http://weather.gov/xml/current_obs/KCEW.xml</xml_url> 
</station> 
+0

你能提供實際的XML請。 – 3xil3

+0

剛剛添加我的xml。 – David

+0

不確定它是否爲複製粘貼錯誤或由設計人員滿意,但未關閉。如果是通過設計,這意味着我們期望有一個wx_station_index元素,我們期望找到這個元素。在你的java代碼中,我們正在處理一個站列表(如果你只有站內元素,這將是正確的) – 3xil3

回答

2

假設wx_station_index是不是一個錯字。

我已經添加了一些註釋來指示jaxb的哪部分xml映射您的代碼的哪部分。將StationListTest更改爲以下版本應該會更好。

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

//create list for all weather stations 
@XmlRootElement(name="wx_station_index") 
public class StationListTest { 

/** 
* @param args 
*/ 
@XmlElement(name="station")  
private ArrayList<StationTest> StationList; 

public StationListTest(){ 
    StationList=new ArrayList<StationTest>(); 
} 

public void setStationListTest(ArrayList<StationTest> StationList){ 
    this.StationList = StationList; 
} 

public ArrayList<StationTest> getStationListTest(){ 
    return StationList; 
} 

} 
+0

嘿大家。謝謝您的幫助。代碼正在工作。 – David

1

有指定屬性StationList之間您StationListTest類和XML文檔中的隱含station列表屬性不匹配。這需要通過明確界定哪些要素的XML的StationList財產從填充來解決:

@XmlElement(name = "station") 
private ArrayList<StationTest> StationList; 

其他一些快速說明:

  • 您應該遵循Java的命名方式(駝峯)類和變量名稱。例如,StationList應該是stationList)。
  • 作爲一般規則,您應該使用@XMLRootElement註釋所有頂級數據類。
  • 作爲一般規則,在定義Java類時(List而不是ArrayList),應該使用類型的最通用形式。
  • 您應表明其他類涉及您的映象與@XmlSeeAlso

下面的類定義你的XML工作:

@XmlRootElement(name = "station") 
@XmlAccessorType(XmlAccessType.FIELD) 
public class StationTest { 

    @XmlElement(name = "station_id") 
    String stationId; 

    String state; 

    @XmlElement(name = "station_name") 
    String stationName; 

    double latitude; 
    double longitude; 

    @XmlElement(name = "html_url") 
    String htmlUrl; 

    // Constructor, getters, setters 
} 

@XmlRootElement(name = "wx_station_index") 
@XmlSeeAlso(value = StationTest.class) 
@XmlAccessorType(XmlAccessType.FIELD) 
public class StationListTest { 
    @XmlElement(name = "station") 
    private List<StationTest> stationList; 

    // Constructors, getters, setters 
} 
+0

謝謝。我有一個問題:對於@XmlAccessorType(XmlAccessType。FIELD)' - 我得到一個錯誤,XMLAccessType不能被解析爲一個變量。如果我註釋掉並嘗試運行我的整個程序,我的錯誤是這樣的:線程「main」中的異常javax.xml.bind.UnmarshalException:意外的元素(uri:「」,local:「wx_station_index」)。預期的元素是,<{} stationTest> – David

+2

您需要導入'javax.xml.bind.annotation.XmlAccessType'。如果您使用的是Eclipse,您可以按Ctrl-Shift-o(或Mac上的Command-Shift-o)來組織導入。其他IDE具有類似的功能。 – Perception

+0

好的。完成。但我仍然得到錯誤:異常在線程「main」javax.xml.bind.UnmarshalException:意外元素(uri:「」,local:「wx_station_index」)。預期的元素是,<{} stationTest> - 感謝所有相同。 – David