3

私は JAXB を使用して、次の気象観測所情報を含む XML ファイルを解析するプロジェクトに取り組んでいます: 名前、観測所 ID、緯度/経度、および URL。今のところ、気象ステーションを名前だけで印刷して、すべてをテストするつもりです。コードをデバッグしましたが、実行しようとすると、次のエラーが表示されます。

スレッド「メイン」での例外 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;

}

} import 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>
4

2 に答える 2

1

StationListクラスの名前付きプロパティとXML ドキュメントStationListTestの暗黙的なリスト属性が一致しません。これは、XML のどの要素からプロパティを設定stationするかを明示的に定義して解決する必要があります。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
}
于 2013-04-14T20:32:52.373 に答える