Page 1 of 3 123 LastLast
Results 1 to 20 of 50
  1. #1
    Aseem is offline Senior Member
    Join Date
    Mar 2009
    Location
    USA
    Posts
    127
    Rep Power
    0

    Default urgent help needed in xml - jaxb

    Hello all, i need to parse xml using jaxb.
    I created java class with jaxb binding wizard and tried to unmarshal but i am constantly getting errors. Can anyone be help please?


    XSD:
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xs:element name="passenger">
    <xs:complexType>
    <xs:attribute name="firstName" use="required">
    <xs:simpleType>
    <xs:restriction base="xs:string"/>
    </xs:simpleType>
    </xs:attribute>
    <xs:attribute name="lastName" use="required">
    <xs:simpleType>
    <xs:restriction base="xs:string"/>
    </xs:simpleType>
    </xs:attribute>
    </xs:complexType>
    </xs:element>
    <xs:element name="passengers">
    <xs:complexType>
    <xs:sequence>
    <xs:element ref="passenger" maxOccurs="unbounded"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    <xs:element name="reservation">
    <xs:complexType>
    <xs:sequence>
    <xs:element ref="passengers"/>
    <xs:element ref="segments"/>
    </xs:sequence>
    <xs:attribute name="refNumber" use="required">
    <xs:simpleType>
    <xs:restriction base="xs:string"/>
    </xs:simpleType>
    </xs:attribute>
    </xs:complexType>
    </xs:element>
    <xs:element name="reservations">
    <xs:complexType>
    <xs:sequence>
    <xs:element ref="reservation" maxOccurs="unbounded"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    <xs:element name="segment">
    <xs:complexType>
    <xs:attribute name="origin" use="required">
    <xs:simpleType>
    <xs:restriction base="xs:string"/>
    </xs:simpleType>
    </xs:attribute>
    <xs:attribute name="destination" use="required">
    <xs:simpleType>
    <xs:restriction base="xs:string"/>
    </xs:simpleType>
    </xs:attribute>
    <xs:attribute name="date" use="required">
    <xs:simpleType>
    <xs:restriction base="xs:string"/>
    </xs:simpleType>
    </xs:attribute>
    </xs:complexType>
    </xs:element>
    <xs:element name="segments">
    <xs:complexType>
    <xs:sequence>
    <xs:element ref="segment" maxOccurs="unbounded"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    <xs:element name="passengerTrips">
    <xs:complexType>
    <xs:sequence>
    <xs:element ref="passenger"/>
    <xs:element ref="trips"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    <xs:element name="trip">
    <xs:complexType>
    <xs:attribute name="origin" use="required">
    <xs:simpleType>
    <xs:restriction base="xs:string"/>
    </xs:simpleType>
    </xs:attribute>
    <xs:attribute name="destination" use="required">
    <xs:simpleType>
    <xs:restriction base="xs:string"/>
    </xs:simpleType>
    </xs:attribute>
    <xs:attribute name="refNumber" use="required">
    <xs:simpleType>
    <xs:restriction base="xs:string"/>
    </xs:simpleType>
    </xs:attribute>
    </xs:complexType>
    </xs:element>
    <xs:element name="trips">
    <xs:complexType>
    <xs:choice>
    <xs:element ref="passengerTrips" maxOccurs="unbounded"/>
    <xs:element ref="trip" maxOccurs="unbounded"/>
    </xs:choice>
    </xs:complexType>
    </xs:element>
    </xs:schema>


    XML

    <?xml version="1.0" encoding="UTF-8"?>
    <reservations>
    <reservation refNumber="a1b2c3">
    <passengers>
    <passenger firstName="gabi" lastName="scott"/>
    <passenger firstName="betty" lastName="marley"/>
    </passengers>
    <segments>
    <segment origin="LAX" destination="MSP" date="2009-08-02 08:30:00.0 CDT"/>
    <segment origin="MSP" destination="ORD" date="2009-08-02 12:30:00.0 CDT"/>
    <segment origin="ORD" destination="MSP" date="2009-08-05 08:30:00.0 CDT"/>
    <segment origin="MSP" destination="LAX" date="2009-08-05 14:30:00.0 CDT"/>
    </segments>
    </reservation>
    <reservation refNumber="d1e2f3">
    <passengers>
    <passenger firstName="linda" lastName="marley"/>
    <passenger firstName="betty" lastName="marley"/>
    </passengers>
    <segments>
    <segment origin="DEN" destination="LAX" date="2009-09-30 17:25:00.0 CDT"/>
    <segment origin="LAX" destination="DEN" date="2009-09-12 09:15:00.0 CDT"/>
    </segments>
    </reservation>
    <reservation refNumber="g1h2i3">
    <passengers>
    <passenger firstName="laura" lastName="tillman"/>
    </passengers>
    <segments>
    <segment origin="CLE" destination="MSP" date="2009-08-02 08:30:00.0 CDT"/>
    <segment origin="MSP" destination="CAK" date="2009-08-05 10:30:00.0 CDT"/>
    <segment origin="CAK" destination="CLE" date="2009-08-05 14:30:00.0 CDT"/>
    </segments>
    </reservation>
    <reservation refNumber="j1k2l3">
    <passengers>
    <passenger firstName="hillary" lastName="wilson"/>
    </passengers>
    <segments>
    <segment origin="JFK" destination="SFO" date="2009-08-02 08:30:00.0 CDT"/>
    <segment origin="SFO" destination="LAX" date="2009-08-02 10:30:00.0 CDT"/>
    <segment origin="SFO" destination="JFK" date="2009-08-05 14:30:00.0 CDT"/>
    </segments>
    </reservation>
    </reservations>

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    What error are you getting?
    We're not mind readers...

    ALso, could you format that stuff and stick it in code tags?

  3. #3
    Menre is offline Member
    Join Date
    Feb 2008
    Posts
    79
    Rep Power
    0

    Default

    Hi,

    This looks like a serious project, but as someone has already asked, no one knows what errors you are getting. Could you point the error message out?

    Menre

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Since Aseem hasn't replied in days, one can only imagine just how "urgent" this whole mess is.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Probably cross posted somewhere else where they got an answer...

  6. #6
    Aseem is offline Senior Member
    Join Date
    Mar 2009
    Location
    USA
    Posts
    127
    Rep Power
    0

    Default

    no guys..!! its a question for my job. And i am due in couple of days.

    - i generated classes from xsd using jaxb binding.
    - i am struggling to write jaxb class for parsing xml.
    Help please.

  7. #7
    Aseem is offline Senior Member
    Join Date
    Mar 2009
    Location
    USA
    Posts
    127
    Rep Power
    0

    Default

    XSD:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xs:element name="passenger">
    <xs:complexType>
    <xs:attribute name="firstName" use="required">
    <xs:simpleType>
    <xs:restriction base="xs:string"/>
    </xs:simpleType>
    </xs:attribute>
    <xs:attribute name="lastName" use="required">
    <xs:simpleType>
    <xs:restriction base="xs:string"/>
    </xs:simpleType>
    </xs:attribute>
    </xs:complexType>
    </xs:element>
    <xs:element name="passengers">
    <xs:complexType>
    <xs:sequence>
    <xs:element ref="passenger" maxOccurs="unbounded"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    <xs:element name="reservation">
    <xs:complexType>
    <xs:sequence>
    <xs:element ref="passengers"/>
    <xs:element ref="segments"/>
    </xs:sequence>
    <xs:attribute name="refNumber" use="required">
    <xs:simpleType>
    <xs:restriction base="xs:string"/>
    </xs:simpleType>
    </xs:attribute>
    </xs:complexType>
    </xs:element>
    <xs:element name="reservations">
    <xs:complexType>
    <xs:sequence>
    <xs:element ref="reservation" maxOccurs="unbounded"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    <xs:element name="segment">
    <xs:complexType>
    <xs:attribute name="origin" use="required">
    <xs:simpleType>
    <xs:restriction base="xs:string"/>
    </xs:simpleType>
    </xs:attribute>
    <xs:attribute name="destination" use="required">
    <xs:simpleType>
    <xs:restriction base="xs:string"/>
    </xs:simpleType>
    </xs:attribute>
    <xs:attribute name="date" use="required">
    <xs:simpleType>
    <xs:restriction base="xs:string"/>
    </xs:simpleType>
    </xs:attribute>
    </xs:complexType>
    </xs:element>
    <xs:element name="segments">
    <xs:complexType>
    <xs:sequence>
    <xs:element ref="segment" maxOccurs="unbounded"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    <xs:element name="passengerTrips">
    <xs:complexType>
    <xs:sequence>
    <xs:element ref="passenger"/>
    <xs:element ref="trips"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    <xs:element name="trip">
    <xs:complexType>
    <xs:attribute name="origin" use="required">
    <xs:simpleType>
    <xs:restriction base="xs:string"/>
    </xs:simpleType>
    </xs:attribute>
    <xs:attribute name="destination" use="required">
    <xs:simpleType>
    <xs:restriction base="xs:string"/>
    </xs:simpleType>
    </xs:attribute>
    <xs:attribute name="refNumber" use="required">
    <xs:simpleType>
    <xs:restriction base="xs:string"/>
    </xs:simpleType>
    </xs:attribute>
    </xs:complexType>
    </xs:element>
    <xs:element name="trips">
    <xs:complexType>
    <xs:choice>
    <xs:element ref="passengerTrips" maxOccurs="unbounded"/>
    <xs:element ref="trip" maxOccurs="unbounded"/>
    </xs:choice>
    </xs:complexType>
    </xs:element>
    </xs:schema>

  8. #8
    Aseem is offline Senior Member
    Join Date
    Mar 2009
    Location
    USA
    Posts
    127
    Rep Power
    0

    Default

    errors i am getting is:
    using following statement:
    Passenger item = (Passenger)((JAXBElement)u.unmarshal(new FileInputStream("C:/Users/admin/Documents/NetBeansProjects/Brt/src/test/demo/input.xml"))).getValue();


    Exception in thread "main" java.lang.ClassCastException: generated.Reservations cannot be cast to javax.xml.bind.JAXBElement


    i tried to fix it with

    Reservation item = (Reservation)((JAXBElement)u.unmarshal(new StreamSource(new StringReader("C:/Users/admin/Documents/NetBeansProjects/Brt/src/test/demo/input.xml")))).getValue();

    error:
    javax.xml.bind.UnmarshalException
    - with linked exception:
    [org.xml.sax.SAXParseException: Content is not allowed in prolog.]

    i am tired...cant fix it.

    Can someone write a jaxb appreciate code for me? I would really appreciate. Your help might help me to get a job.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    unmarshal() will give you a JAXB class of the type based on the top level XML element. In your case (as the exception says) that's a Reservation. Casting to JAXBElement is wrong.

  10. #10
    Aseem is offline Senior Member
    Join Date
    Mar 2009
    Location
    USA
    Posts
    127
    Rep Power
    0

    Default

    i get the same error using

    Reservation item = (Reservation)((JAXBElement)u.unmarshal(new FileInputStream("C:/Users/admin/Documents/NetBeansProjects/Brt/src/test/demo/input.xml"))).getValue();

    how do i get the other elements then besides reservation?
    plz help.

    thanks

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    No!
    You are still trying to cast the result of unmarshal() to a JAXBElement.
    Java Code:
    (Reservation)u.unmarshal(new FileInputStream("C:/Users/admin/Documents/NetBeansProjects/Brt/src/test/demo/input.xml"));
    // Stuff to make it visible?
    As for the other elements, you defined the xsd so you should know what a Resevation contains.

  12. #12
    Aseem is offline Senior Member
    Join Date
    Mar 2009
    Location
    USA
    Posts
    127
    Rep Power
    0

    Default

    same error .....

    Exception in thread "main" java.lang.ClassCastException: generated.Reservations cannot be cast to generated.Reservation

    i m really tired. Tolls, be my help.

  13. #13
    Aseem is offline Senior Member
    Join Date
    Mar 2009
    Location
    USA
    Posts
    127
    Rep Power
    0

    Default

    When you write

    Reservations item=(Reservations)u.unmarshal(new FileInputStream("C:/Users/admin/Documents/NetBeansProjects/Brt/src/test/demo/input.xml"));

    Does it unmarshals whole xml and stores the result in item?

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    unmarshal() unmarshals the whole content of input.xml. What else can it do?

    OK, so it should be (Reservations)...but you should know that since you have the xml, and the xsd! All I am doing is trying to point you in the right direction, at least in terms of what unmarshall() does.

  15. #15
    Aseem is offline Senior Member
    Join Date
    Mar 2009
    Location
    USA
    Posts
    127
    Rep Power
    0

    Default

    Now, how do you fetch the each data from item variable in above case. It compiles but its giving weird values.

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Do you know how JAXB works?
    What is it you're attempting to do?

  17. #17
    Aseem is offline Senior Member
    Join Date
    Mar 2009
    Location
    USA
    Posts
    127
    Rep Power
    0

    Default

    Thank you Tolls.
    Now, i want to list all the data in input.xml.
    Seems like it is parsed. now, i wanna display all the data.

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    So, you have a Reservations object, that contains objects that represent the XML data in input.xml, defined by your xsd. That's what JAXB does. It's for turning XML into objects you can use easily in your application. It's not for parsing, or querying.

    So again, what are you trying to achieve?

  19. #19
    Aseem is offline Senior Member
    Join Date
    Mar 2009
    Location
    USA
    Posts
    127
    Rep Power
    0

    Default

    This is my Registration class.

    public class Registration {

    private Reservations resev = null;
    private JAXBContext jaxbContext = null;
    private Unmarshaller unmarshaller = null;

    public Registration() {
    try {
    jaxbContext = JAXBContext.newInstance("generated");
    unmarshaller = jaxbContext.createUnmarshaller();
    } catch (JAXBException e) {
    }
    }

    @SuppressWarnings("unchecked")
    public List loadXML(InputStream istrm) {
    try {
    Object o = unmarshaller.unmarshal(istrm);

    if(resev== null) {
    resev = (Reservations)o;
    return(resev.getReservation());
    }

    } catch (JAXBException e) {
    e.printStackTrace();
    }

    return null;
    }
    }

    This is my RegistrationList Class used for displaying the data stored in XML.


    public class RegistrationList {
    public static void main(String[] args) {
    Registration xmgr = new Registration();
    {
    File file = new File("C:/Users/admin/Documents/NetBeansProjects/Brt/src/test/demo/input.xml");
    List rtList = new ArrayList();

    try {
    FileInputStream fis = new FileInputStream(file);

    rtList = xmgr.loadXML(fis);

    Iterator rtItr = rtList.iterator();
    while(rtItr.hasNext()){
    Reservation item = (Reservation) rtItr.next();

    System.out.println("Registration Number: = " + item.getRefNumber());

    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }
    }
    }}

    It prints Registration Number but I am trying to print PassengerName, Origin, Destination, date for each registration number. Until now, i can only get registration number. How can i list all?

  20. #20
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Have you looked at the Reservation clas that's been generated?
    Assuming JAXB has created these correctly it will have your passengers and segments in there, as defined in the xsd. This is why I ask if you know what JAXB does...

Page 1 of 3 123 LastLast

Similar Threads

  1. Urgent Help Needed
    By Cid17 in forum New To Java
    Replies: 10
    Last Post: 07-03-2009, 02:42 PM
  2. Urgent Help needed!
    By mlwong in forum New To Java
    Replies: 0
    Last Post: 03-19-2009, 08:51 AM
  3. hi help needed, this is urgent
    By msciriha in forum NetBeans
    Replies: 1
    Last Post: 02-07-2009, 06:16 PM
  4. Urgent help needed here pls!!
    By Manfizy in forum NetBeans
    Replies: 5
    Last Post: 01-28-2009, 07:38 AM
  5. Replies: 0
    Last Post: 06-03-2008, 11:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •