从 XML 消息中查找模式,生成 class 并在 JAVA 中反序列化 XML

Finding schema from XML message, generate class and deserialize in JAVA from XML

我是 JAVA 的新手,遇到了一个非常具体的问题:

我有一个 return 定期 XML 消息的网络服务。 我希望能够将此消息反序列化为 JAVA 对象,但我不知道如何进一步处理...

这是我收到的基本 xml 消息:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ds:tfmDataService
  xmlns:ds="urn:us:gov:dot:faa:atm:tfm:tfmdataservice"
  xmlns="urn:us:gov:dot:faa:atm:tfm:tfmdataservice"
  xmlns:fdm="urn:us:gov:dot:faa:atm:tfm:flightdata"
  xmlns:nxce="urn:us:gov:dot:faa:atm:tfm:tfmdatacoreelements"
  xmlns:nxcm="urn:us:gov:dot:faa:atm:tfm:flightdatacommonmessages"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:us:gov:dot:faa:atm:tfm:tfmdataservice TFMData_Service.xsd">
  <fltdOutput>
    <fdm:fltdMessage acid="UAL1252" airline="UAL" arrArpt="KIAH" cdmPart="true" depArpt="KSAT" fdTrigger="FD_FLIGHT_MODIFY_MSG" flightRef="125539563" msgType="FlightModify" sensitivity="A" sourceFacility="UAL" sourceTimeStamp="2020-01-27T09:32:11Z">
      <fdm:ncsmFlightModify>
        <nxcm:qualifiedAircraftId>
          <nxce:aircraftId>UAL1252</nxce:aircraftId>
          <nxce:computerId>
            <nxce:facilityIdentifier>UAL</nxce:facilityIdentifier>
          </nxce:computerId>
          <nxce:igtd>2020-01-27T23:30:00Z</nxce:igtd>
          <nxce:departurePoint>
            <nxce:airport>KSAT</nxce:airport>
          </nxce:departurePoint>
          <nxce:arrivalPoint>
            <nxce:airport>KIAH</nxce:airport>
          </nxce:arrivalPoint>
        </nxcm:qualifiedAircraftId>
        <nxcm:airlineData>
          <nxcm:flightStatusAndSpec>
            <nxcm:flightStatus>FILED</nxcm:flightStatus>
            <nxcm:aircraftModel>B739</nxcm:aircraftModel>
            <nxcm:aircraftspecification aircraftEngineClass="JET" numberOfAircraft="1">B739</nxcm:aircraftspecification>
          </nxcm:flightStatusAndSpec>
          <nxcm:eta etaType="SCHEDULED" timeValue="2020-01-28T00:28:00Z"/>
          <nxcm:etd etdType="SCHEDULED" timeValue="2020-01-27T23:42:00Z"/>
          <nxcm:flightTimeData airlineInTime="2020-01-28T00:37:00Z" airlineOffTime="2020-01-27T23:42:00Z" airlineOnTime="2020-01-28T00:28:00Z" airlineOutTime="2020-01-27T23:30:00Z" flightCreation="2020-01-26T23:30:38Z" originalArrival="2020-01-28T00:28:00Z" originalDeparture="2020-01-27T23:42:00Z"/>
          <nxcm:diversionIndicator>NO_DIVERSION</nxcm:diversionIndicator>
          <nxcm:rvsmData currentCompliance="true" equipped="true" futureCompliance="true"/>
          <nxcm:arrivalFixAndTime arrTime="2020-01-28T00:03:10Z" fixName="GMANN"/>
        </nxcm:airlineData>
      </fdm:ncsmFlightModify>
    </fdm:fltdMessage>
  </fltdOutput>
</ds:tfmDataService>

所以首先我不知道我应该如何找到架构位置,因为地址是 urn:us:gov:dot:faa:atm:tfm:tfmdataservice TFMData_Service.xsd

我不知道如何获得这个模式描述,因为这个地址看起来不像一个 http 地址,所以我无法提取模式描述,因为我不知道从哪里下载它。

我知道我需要这个模式来生成我的 java class,它将用于将消息反序列化为 JAVA 对象。 是否可以在收到此类消息时即时生成对象,或者我真的需要先生成 classes 来反序列化我的消息吗?

然后,如果我能够获得 xsd 描述收到的任何 xml 消息,我应该如何生成数据反序列化所需的代码?它是如何工作的 ?是否有特定的工具可以做到这一点?

提前致谢!

So first i don't get how i'm supposed to find the schema location since the adress is urn:us:gov:dot:faa:atm:tfm:tfmdataservice TFMData_Service.xsd

I have no clue on how to get this schema description since this adress doesnt looks like a http adress and so i'm not able to extract the schema description since i don't know where to download it.

并非所有架构都在 public 域中。通常,您需要先与提供商组织签约,然后才能获得架构 and/or API 规范。

I understood that i need this schema in order to generate my java class that will be used to deserialize the message into a JAVA object. Is it possible to generate the object on the fly when receiving this type of message or do i really need to first generate the classes to deserialized my messages ?

Java 可以在不使用架构的情况下解析 XML,但我不建议在这种情况下使用它(从政府 API 接收数据)。如果有可用的模式——如果没有,我会感到非常惊讶——那么你应该使用它。它将简化您的 Java 代码,并且您还可以选择在解析时根据架构进行验证。

Then if i'm able to get the xsd describing any xml message received, how am i supposed to generate the code necessary for data deserialization ? How does it work ? Are there specific tools to do that ?

使用您最喜欢的搜索引擎很容易回答这个问题。搜索 'JAXB'.