[英]How to parse xml using jaxb
嗨,我正在嘗試獲取xml數據..結構如下:....
<transport>
<Journey>
<JourneyPatternSection id ="JPS_13">
<from> a</from>
<to>b</to>
</JourneypatternSection>
</Journey>
<JourneyPattern id="JP_1_0">
<JourneyPatternSectionRefs>JPS_13</JourneyPatternSectionRefs>
</JourneyPattern>
<VechileJourney>
<JourneyPatternRef>JP_1_0</JourneyPatternRef>
<DepartureTime>17:10:00</DepartureTime>
</VechileJourney>
</transport>
我已經使用jaxb提取了JourneypatternId,但是由於vechilejourney標簽中的旅程模式id有所反映,因此我無法獲取出發時間和往返信息。
從http://www.thaiopensource.com/relaxng/trang-manual.html下載trang.jar轉到命令提示符,要將xml轉換為xsd,請鍵入以下命令
java -jar trang.jar transport.xml transport.xsd
將您的xml結構轉換為xsd,並編寫以下命令后,
xjc -p com.jaxb.test.xml.beans transport.xsd
上面的命令將從您的transport.xsd中生成Java bean
之后,您可以如下解組XML
try
{
final JAXBContext jc = JAXBContext.newInstance(Transport.class);
final Unmarshaller unmarshaller = jc.createUnmarshaller();
try
{
final Transport transport = (Transport) unmarshaller.unmarshal(new FileReader(TRANSPORT_XML));
} catch (final FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (final JAXBException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
參考: http : //shahpritesh.blogspot.in/2012/06/writing-and-reading-java-object-to-and.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.