1

您好我正在尝试获取 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 标签中有参考。

4

1 回答 1

2

从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

于 2013-06-18T11:58:42.593 回答