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.

1 Answer 1

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()

Sign up to request clarification or add additional context in comments.

5 Comments

can i use jaxb for this? , and how can i map this xml with corresponding java class(Bean)?
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 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.
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?
The tutorial jaxb.java.net/tutorial seems pretty complete, but are you sure you have to generate JavaBeans?
yes, i want to use that bean to communicate with db pluse use in over a project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.