gpt4 book ai didi

java - 如何使用jaxb解析xml

转载 作者:行者123 更新时间:2023-11-30 04:20:33 25 4
gpt4 key购买 nike

嗨,我正在尝试获取 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,但我无法获取出发时间和往返信息,因为旅程模式 id 在 vechilejourney tag 中有引用。

最佳答案

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 beans

之后,您可以按如下方式解码您的 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

关于java - 如何使用jaxb解析xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17167565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com