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

    Default

    yeah, reservation class has been created.
    but using get method, i can only retrieve registration number. using get method on reservation instance i.e. item like item.getPassengers() and item.getSegments() leads to garbage value.

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

    Default

    So it has getPassengers() and getSegments() methods then? Which return what? "Garbage" isn't terribly descriptive.
    What code are you using to write out the stuff returned from getPassengers, for example.

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

    Default

    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());
    System.out.println("Registration Number: = " + item.getPassengers());
    System.out.println("Registration Number: = " + item.getSegments());
    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }
    }
    }}

    // only the registration number works. rest two dont work. returns garbage values.

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

    Default

    reservation class:

    package generated;

    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlType;


    /**
    * <p>Java class for anonymous complex type.
    *
    * <p>The following schema fragment specifies the expected content contained within this class.
    *
    * <pre>
    * &lt;complexType>
    * &lt;complexContent>
    * &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
    * &lt;sequence>
    * &lt;element ref="{}passengers"/>
    * &lt;element ref="{}segments"/>
    * &lt;/sequence>
    * &lt;attribute name="refNumber" use="required">
    * &lt;simpleType>
    * &lt;restriction base="{http://www.w3.org/2001/XMLSchema}string">
    * &lt;/restriction>
    * &lt;/simpleType>
    * &lt;/attribute>
    * &lt;/restriction>
    * &lt;/complexContent>
    * &lt;/complexType>
    * </pre>
    *
    *
    */
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
    "passengers",
    "segments"
    })
    @XmlRootElement(name = "reservation")
    public class Reservation {

    @XmlElement(required = true)
    protected Passengers passengers;
    @XmlElement(required = true)
    protected Segments segments;
    @XmlAttribute(required = true)
    protected String refNumber;

    /**
    * Gets the value of the passengers property.
    *
    * @return
    * possible object is
    * {@link Passengers }
    *
    */
    public Passengers getPassengers() {
    return passengers;
    }

    /**
    * Sets the value of the passengers property.
    *
    * @param value
    * allowed object is
    * {@link Passengers }
    *
    */
    public void setPassengers(Passengers value) {
    this.passengers = value;
    }

    /**
    * Gets the value of the segments property.
    *
    * @return
    * possible object is
    * {@link Segments }
    *
    */
    public Segments getSegments() {
    return segments;
    }

    /**
    * Sets the value of the segments property.
    *
    * @param value
    * allowed object is
    * {@link Segments }
    *
    */
    public void setSegments(Segments value) {
    this.segments = value;
    }

    /**
    * Gets the value of the refNumber property.
    *
    * @return
    * possible object is
    * {@link String }
    *
    */
    public String getRefNumber() {
    return refNumber;
    }

    /**
    * Sets the value of the refNumber property.
    *
    * @param value
    * allowed object is
    * {@link String }
    *
    */
    public void setRefNumber(String value) {
    this.refNumber = value;
    }

    }

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

    Default

    Does Passengers have a toString() method? If not, then all you'll get from the println() is a hash value (I think), or whatever the default Object.toString() is.

  6. #26
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    Are you 100% sure that xml file you posted is well generated
    from that xsd???

    I'm not at all !

    Warning:There are no grammar constraints DTD or XML shema detected for the document!

    Try to create you own xml using dtd (*.xsd)
    and set manually your root element and all optional attributes.
    Validate it after creation and then put it in your code for a test.


    From example.xsd create a abc.xml
    In Eclipse
    (New => Other => XML => abc.xml
    (in XML choose wizard Create XML file from schema file and browse to /data/example.xsd)

    Now choose 'reservation' as root element.
    Wizard will create empty xml so put in some values for
    refNumber
    passenger
    and
    segments

    Here is what you get as abc.xml:


    --

    Java Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <reservations xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../data/example.xsd">
      <reservation refNumber="123">
        <passengers>
          <passenger firstName="Aseem" lastName="The King"/>
        </passengers>
        <segments>
          <segment date="" destination="NYC" origin=""/>
        </segments>
      </reservation>
    </reservations>
    --


    Try this in your test code:

    Java Code:
    			String xmlPath = "C:\\workspace\\JaxbAseem\\src\\jaxbaseem\\xml\\[B]abc.xml[/B]";
    			
    			//Object obj = unm.unmarshal(new FileInputStream(xmlPath));
    
    			javax.xml.bind.JAXBElement<Reservation> res =  (JAXBElement<Reservation>) unm.unmarshal(new FileInputStream(xmlPath));
    
    			
    			Reservation someReservation = res.getValue();
    			System.out.println(someReservation.getRefNumber());

    Do you still have class cast problems???

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

    Default

    it doesnt have a toString method. I pasted Reservation method above. I only get the reference number.

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

    Default

    My test Code:

    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());
    System.out.println("Registration Number: = " + item.getPassengers());
    System.out.println("Registration Number: = " + item.getSegments());
    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }
    }
    }}

    It prints only Registration NUmber, but requirement is to get Passenger FirstName, lastName,Origin, Destination, date for each reference number.

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

    Default

    Fon,

    Cast problem is gone, all thanks to Tolls.
    But i am not getting PassenerName, Origin, Destination and Date. I am only getting ref number,

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

    Default

    Because Passengers, at a guess, only contains a list of Passengers, so you'd have to iterate over that. Same with Segments.

    Come on, do some reading...look at the generated classes!

  11. #31
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    It;s not big deal at all:
    Java Code:
    String xmlPath = "C:\\workspace\\JaxbAseem\\src\\jaxbaseem\\xml\\given_xml.xml";
    
    			//Object obj = unm.unmarshal(new FileInputStream(xmlPath));
    
    			javax.xml.bind.JAXBElement<Reservations> res =  (JAXBElement<Reservations>) unm.unmarshal(new FileInputStream(xmlPath));
    
    
    			/*root*/
    			Reservations someReservations = res.getValue();
    
    			List<Reservation> listRes =  someReservations.getReservation();
    
    			/*root reservation has list of reservations*/
    
    			/*each reservation has:
    			 * 1. refNumber, 
    			 * 2. Passengers (with inner list)
    			 * 3. and Segments(with inner list)*/
    
    			for (Reservation one : listRes){
    
    				/*1. ref*/	
    				System.out.println("Ref:" + one.getRefNumber() );
    
    				/*2 Pessengers*/
    				Passengers passengers = one.getPassengers();
    				List<Passenger> passengerList =  passengers.getPassenger();
    
    				for (Passenger onePass: passengerList){
    					System.out.println( "name:" +  onePass.getFirstName() );
    					System.out.println( "last" + onePass.getLastName() );
    				}
    
    				
    				/*3 Segments*/
    				Segments segments =  one.getSegments();
    				List<Segment> segmentList = segments.getSegment();
    				
    				for(Segment oneSegment : segmentList){
    					System.out.println(oneSegment.getDate());
    					System.out.println(oneSegment.getDestination() );
    					System.out.println(oneSegment.getOrigin());
    				}
    				
    
    			}//for
    is it OK?

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

    Default

    I am iterating. i dont know what the problem is. I have been trying to get it done for a long time. Help me tolls. I already pasted Reservation class and my codes. i am new to Jaxb.

  13. #33
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    Aseem relax :)

    If unmarshall is done well,
    there are no more problems with JAXb
    now it's just regular java classes with some Lists and
    iteration job that I have posted for you completelly

    try it and see how it works it's piece of cake really ;)

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

    Default

    thanks fon, i am checking it out.

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

    Default

    But you aren't iterating over the passengers or the segments!
    See FON's code for what I mean.

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

    Default

    there is error:

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

    while running Fon's code.

    Same old error.

  17. #37
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    Don't use that part!

    I had to post it just to make it clear how things work.

    Use your code to get Reservations instance.

    When you get it,
    use my code from line:

    List<Reservation> listRes = someReservations.getReservation();

    where someReservations is yours instance.

    come on man

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

    Default

    Fon, Thank you very much. Its done. You are awesome.
    tolls, Thank you very much.

  19. #39
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    A-a you won't get a way so easily :)

    Atlantic ocean of beer for me and Pacific Ocean of [fill in] for Mr. Tolls :D

    good luck and let's us know if you get a job :)

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

Page 2 of 3 FirstFirst 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
  •