Reputation: 161
Hi I am trying to fetch xml data .. Structure is like this : ....
<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>
I have extracted JourneypatternId using jaxb but I am not able to get the departure time and to and from information as journey pattern id has refrence in vechilejourney tag .
Upvotes: 1
Views: 1356
Reputation: 887
Download trang.jar from http://www.thaiopensource.com/relaxng/trang-manual.html Go to the command prompt, To Convert your xml to xsd, type below command
java -jar trang.jar transport.xml transport.xsd
After converting your xml structure to xsd, and write below command,
xjc -p com.jaxb.test.xml.beans transport.xsd
Above command will generate java beans from your transport.xsd
After that you can unmarshall your xml as below
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();
}
Reference: http://shahpritesh.blogspot.in/2012/06/writing-and-reading-java-object-to-and.html
Upvotes: 2