简体   繁体   English

使用JAXB将XML解析为Java变量(beans)

[英]parsing XML to java variables(beans) using JAXB

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? 我可以为此使用jaxb吗?

i am using this file to parse which i am getting on server response. 我正在使用此文件来解析服务器响应。 what i nned to do for parsing it using JAXB. 我打算使用JAXB进行解析。 am getting it in String format.and how can i directly map into java class variables or beans? 正在以String格式获取它。如何直接映射到Java类变量或bean? thanx. 谢谢。

You can see this question: How do I load an org.w3c.dom.Document from XML in a string? 您会看到以下问题: 如何从XML字符串中加载org.w3c.dom.Document?

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. 在将XML字符串加载到DOM文档中之后,您可以调用getElementsByTagName方法来检索特定标签,然后对其进行迭代,使用getNodeValue来提取标签内的值,并使用getAttribute来提取属性。

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() 您无法在运行时轻松地将其映射到具有desider字段的Java类,但是可以使用诸如getRooms()类的简单方法将元素的解析和提取包装在类中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM