1

Am working with the server response with my application. am getting this stuff as response as string.

<Body>
  <HotelRQ xmlns="urn:Hotel_Search">
<POS>
         <Source Username='USERNAME' Password='PASSWORD' PropertyID='PROPERTYID' />
</POS>
<AvailRequests>
     <AvailRequest>
      <StayDateRange Start='2009-09-05T12:00:00' End='2009-09-06T12:00:00'/>
      <RoomStays>
            <RoomStay> <!—for Room 1->
                <GuestCounts>
                    <GuestCount Count='2'/>
                </GuestCounts>
                <ChildCounts> 
                    <ChildAge Age='10'/>
                    <ChildAge Age='09'/>
                </ChildCounts>
            </RoomStay>

          </RoomStays>
          <SearchCriteria>
        <Criterion>
        <HotelRef HotelCityName='CITY' HotelName='' Area='' Attraction='' Rating=''/>
        <Sorting Preference='2'/>
                                             <ResponseType Compact="Y"/>
        </Criterion>
          </SearchCriteria>
        </AvailRequest>
      </AvailRequests>
    </HotelRQ>
</Body>

can i use jaxb for this?

i am using this file to parse which i am getting on server response. what i nned to do for parsing it using JAXB. am getting it in String format.and how can i directly map into java class variables or beans? thanx.

V.Rohan
  • 945
  • 2
  • 14
  • 27

1 Answers1

0

You can see this question: How do I load an org.w3c.dom.Document from XML in a string?

After loading the XML string inside a DOM document, you can call, among others, the method getElementsByTagName to retrieve specific tags and then iterate over them, using getNodeValue to extract the value inside a tag and getAttribute to extract attributes.

You can't easily map it to a Java class with desider fields at runtime, but you can wrap the parsing and extraction of elements inside a class with simple methods like getRooms()

Community
  • 1
  • 1
Jacopofar
  • 3,407
  • 2
  • 19
  • 29
  • can i use jaxb for this? , and how can i map this xml with corresponding java class(Bean)? – V.Rohan Dec 03 '12 at 09:21
  • 1
    Yes, but you'll need the XML schema to build the JavaBean wrappers with jaxb, so, unless you have it somewhere, you'll have to write the XSD schema or generate one from XML (after removing <!—for Room 1->) at http://www.freeformatter.com/xsd-generator.html which will still need some manual adjustement. If you don't have an XSD schema, maybe it's easier to write the JavaBean class by hand, wrapping getters and setters with DOM methods. – Jacopofar Dec 03 '12 at 11:19
  • sorry, but i dont know how to do. is there any tutorial for it? basically i m getting it from server URLConnection(xml format) as request, then i need to seperate it in wrapper > process it and then response as requirment. but how to map that file and bean? – V.Rohan Dec 03 '12 at 12:09
  • 1
    The tutorial http://jaxb.java.net/tutorial/ seems pretty complete, but are you sure you have to generate JavaBeans? – Jacopofar Dec 03 '12 at 14:02
  • yes, i want to use that bean to communicate with db pluse use in over a project. – V.Rohan Dec 04 '12 at 04:08