我正在开展一个项目,在该项目中我将使用 JAXB 解析包含以下气象站信息的 XML 文件:名称、站 ID、纬度/经度和 url。现在我打算通过仅按名称打印气象站来测试所有内容。我已经调试了我的代码,但是当我尝试运行时:我收到以下错误:
线程“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>
最佳答案
假设 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;
}
}
关于java - JAXB XML 解析问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16003005/
我有一个字符串input="maybe(thisis|thatwas)some((nice|ugly)(day|night)|(strange(weather|time)))"Ruby中解析该字符串的最佳方法是什么?我的意思是脚本应该能够像这样构建句子:maybethisissomeuglynightmaybethatwassomenicenightmaybethiswassomestrangetime等等,你明白了......我应该一个字符一个字符地读取字符串并构建一个带有堆栈的状态机来存储括号值以供以后计算,还是有更好的方法?也许为此目的准备了一个开箱即用的库?
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我正在使用ruby1.9解析以下带有MacRoman字符的csv文件#encoding:ISO-8859-1#csv_parse.csvName,main-dialogue"Marceu","Giveittohimóhe,hiswife."我做了以下解析。require'csv'input_string=File.read("../csv_parse.rb").force_encoding("ISO-8859-1").encode("UTF-8")#=>"Name,main-dialogue\r\n\"Marceu\",\"Giveittohim\x97he,hiswife.\"\
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
由于fast-stemmer的问题,我很难安装我想要的任何rubygem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www