xml与javaBean的转换

本文介绍了如何利用JAXB进行XML与JavaBean之间的转换。首先,通过trang.jar将xml文件转换为xsd文件,然后在项目中引入JAXB的依赖。由于Java9及以上版本不再包含tools.jar,因此推荐使用在线工具如ToolsCat生成JavaBean。在JavaBean上添加相应注解后,封装转换工具类,即可方便地实现XML到Java对象的转化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

xml文件与javaBean之间的相互转换是经常发生的,在这方面的相关jar包也比较多,可是相对而言比较简单的还是JAXB。只需要做到如下几步就可:

1、下载trang.jar 

     这个jar包是根据xml文件生成xsd文件,使用的命令是:Java  -jar   xml文件名.xml   文件名.xsd

    例如:java  -jar  demo-01.xml demo-01.xsd

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <!-- Root element of a model document -->
  <xsd:element name="model">
    <xsd:complexType>
      <!-- model elements -->
      <xsd:sequence>
        <xsd:element name="point"
                     type="pointType"
                     minOccurs="0"
                     maxOccurs="unbounded"/>
        <xsd:element name="path"
                     type="pathType"
                     minOccurs="0"
                     maxOccurs="unbounded"/>
        <xsd:element name="vehicle"
                     type="vehicleType"
                     minOccurs="0"
                     maxOccurs="unbounded"/>
        <xsd:element name="locationType"
                     type="locationTypeType"
                     minOccurs="0"
                     maxOccurs="unbounded"/>
        <xsd:element name="location"
                     type="locationType"
                     minOccurs="0"
                     maxOccurs="unbounded"/>
        <xsd:element name="block"
                     type="blockType"
                     minOccurs="0"
                     maxOccurs="unbounded"/>
        <xsd:element name="visualLayout"
                     type="visualLayoutType"
                     minOccurs="0"
                     maxOccurs="1"/>
        <xsd:element name="property"
                     type="propertyType"
                     minOccurs="0"
                     maxOccurs="unbounded"/>
      </xsd:sequence>
      <!-- model attributes -->
      <xsd:attribute name="version"
                     type="versionType"
                     use="required"/>
      <xsd:attribute name="name"
                     type="xsd:string"
                     use="required"/>
    </xsd:complexType>
  </xsd:element>

  <!-- Simple data type for version strings -->
  <xsd:simpleType name="versionType">
    <xsd:restriction base="xsd:string">
      <xsd:pattern value="\d\.\d\.\d"/>
    </xsd:restriction>
  </xsd:simpleType>
  
  <!-- Data type for point elements -->
  <xsd:complexType name="pointType">
    <xsd:sequence>
      <xsd:element name="outgoingPath"
                   minOccurs="0"
                   maxOccurs="unbounded">
        <xsd:complexType>
          <xsd:attribute name="name"
                         type="xsd:string"
                         use="required"/>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="property"
                   type="propertyType"
                   minOccurs="0"
                   maxOccurs="unbounded"/>
      <xsd:element name="pointLayout"
                   type="pointLayoutType"
                   minOccurs="0"
                   maxOccurs="1"/>
    </xsd:sequence>
    <xsd:attribute name="name"
                   type="xsd:string"
                   use="required"/>
    <xsd:attribute name="xPosition"
                   type="xsd:long"
                   use="required"/>
    <xsd:attribute name="yPosition"
                   type="xsd:long"
                   use="required"/>
    <xsd:attribute name="zPosition"
                   type="xsd:long"/>
    <xsd:attribute name="vehicleOrientationAngle"
                   type="xsd:float"/>
    <xsd:attribute name="type"
                   type="pointTypeType"
                   use="required"/>
  </xsd:complexType>

  <!-- Data type for point layout type elements -->
  <xsd:complexType name="pointLayoutType">
    <xsd:attribute name="xPosition"
                   type="xsd:long"
                   use="required"/>
    <xsd:attribute name="yPosition"
                   type="xsd:long"
                   use="required"/>
    <xsd:attribute name="xLabelOffset"
                   type="xsd:long"
                   use="required"/>
    <xsd:attribute name="yLabelOffset"
                   type="xsd:long"
                   use="required"/>
    <xsd:attribute name="layerId"
                   type="xsd:int"
                   use="required"/>
  </xsd:complexType>
  
  <xsd:simpleType name="pointTypeType">
    <xsd:restriction base="xsd:string">
      <xsd:enumeration value="REPORT_POSITION"/>
      <xsd:enumeration value="HALT_POSITION"/>
      <xsd:enumeration value="PARK_POSITION"/>
    </xsd:restriction>
  </xsd:simpleType>

  <!-- Data type for path elements -->
  <xsd:complexType name="pathType">
    <xsd:sequence>
      <xsd:element name="peripheralOperation"
                   minOccurs="0"
                   maxOccurs="unbounded">
        <xsd:complexType>
          <xsd:attribute name="name"
                         type="xsd:string"
                         use="required"/>
          <xsd:attribute name="locationName"
                         type="xsd:string"
                         use="required"/>
          <xsd:attribute name="executionTrigger"
                         type="executionTriggerType"
                         use="required"/>
          <xsd:attribute name="completionRequired"
                         type="xsd:boolean"
                         use="required"/>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="property"
                   type="propertyType"
                   minOccurs="0"
                   maxOccurs="unbounded"/>
      <xsd:element name="pathLayout"
                   type="pathLayoutType"
                   minOccurs="0"
                   maxOccurs="1"/>
    </xsd:sequence>
    <xsd:attribute name="name"
                   type="xsd:string"
                   use="required"/>
    <xsd:attribute name="sourcePoint"
                   type="xsd:string"
                   use="required"/>
    <xsd:attribute name="destinationPoint"
                   type="xsd:string"
                   use="required"/>
    <xsd:attribute name="length"
                   type="xsd:unsignedInt"
                   use="optional"/>
    <xsd:attribute name="maxVelocity"
                   type="xsd:unsignedInt"
                   use="required"/>
    <xsd:attribute name="maxReverseVelocity"
                   type="xsd:unsignedInt"
                   use="required"/>
    <xsd:attribute name="locked"
                   type="xsd:boolean"
                   use="required"/>
  </xsd:complexType>
  
  <!-- Data type for path layout type elements -->
  <xsd:complexType name="pathLayoutType">
    <xsd:sequence>
      <xsd:element name="controlPoint"
                   type="controlPointType"
                   minOccurs="0"
                   maxOccurs="unbounded"/>
    </xsd:sequence>
    <xsd:attribute name="connectionType"
                   type="connectionTypeType"
                   use="required"/>
    <xsd:attribute name="layerId"
                   type="xsd:int"
                   use="required"/>
  </xsd:complexType>

  <xsd:simpleType name="connectionTypeType">
    <xsd:restriction base="xsd:string">
      <xsd:enumeration value="DIRECT"/>
      <xsd:enumeration value="ELBOW"/>
      <xsd:enumeration value="SLANTED"/>
      <xsd:enumeration value="POLYPATH"/>
      <xsd:enumeration value="BEZIER"/>
      <xsd:enumeration value="BEZIER_3"/>
    </xsd:restriction>
  </xsd:simpleType>
  
  <xsd:complexType name="controlPointType">
    <xsd:attribute name="x"
                   type="xsd:long"
                   use="required"/>
    <xsd:attribute name="y"
                   type="xsd:long"
                   use="required"/>
  </xsd:complexType>

  <xsd:simpleType name="executionTriggerType">
    <xsd:restriction base="xsd:string">
      <xsd:enumeration value="BEFORE_MOVEMENT"/>
      <xsd:enumeration value="AFTER_MOVEMENT"/>
    </xsd:restriction>
  </xsd:simpleType>

  <!-- Data type for vehicle elements -->
  <xsd:complexType name="vehicleType">
    <xsd:sequence>
      <xsd:element name="property"
                   type="propertyType"
                   minOccurs="0"
                   maxOccurs="unbounded"/>
      <xsd:element name="vehicleLayout"
                   type="vehicleLayoutType"
                   minOccurs="0"
                   maxOccurs="1"/>
    </xsd:sequence>
    <xsd:attribute name="name"
                   type="xsd:string"
                   use="required"/>
    <xsd:attribute name="length"
                   type="xsd:unsignedInt"/>
    <xsd:attribute name="energyLevelCritical"
                   type="xsd:unsignedInt"/>
    <xsd:attribute name="energyLevelGood"
                   type="xsd:unsignedInt"/>
    <xsd:attribute name="energyLevelFullyRecharged"
                   type="xsd:unsignedInt"/>
    <xsd:attribute name="energyLevelSufficientlyRecharged"
                   type="xsd:unsignedInt"/>
    <xsd:attribute name="maxVelocity"
                   type="xsd:unsignedInt"/>
    <xsd:attribute name="maxReverseVelocity"
                   type="xsd:unsignedInt"/>
  </xsd:complexType>

  <!-- Data type for vehicle layout type elements -->
  <xsd:complexType name="vehicleLayoutType">
    <xsd:attribute name="color"
                   type="colorType"
                   use="required"/>
  </xsd:complexType>

  <!-- Data type for location type elements -->
  <xsd:complexType name="locationTypeType">
    <xsd:sequence>
      <xsd:element name="allowedOperation"
                   minOccurs="0"
                   maxOccurs="unbounded">
        <xsd:complexType>
          <xsd:attribute name="name"
                         type="xsd:string"
                         use="required"/>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="allowedPeripheralOperation"
                   minOccurs="0"
                   maxOccurs="unbounded">
        <xsd:complexType>
          <xsd:attribute name="name"
                         type="xsd:string"
                         use="required"/>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="property"
                   type="propertyType"
                   minOccurs="0"
                   maxOccurs="unbounded"/>
      <xsd:element name="locationTypeLayout"
                   type="locationTypeLayoutType"
                   minOccurs="0"
                   maxOccurs="1"/>
    </xsd:sequence>
    <xsd:attribute name="name"
                   type="xsd:string"
                   use="required"/>
  </xsd:complexType>

  <!-- Data type for location type layout type elements -->
  <xsd:complexType name="locationTypeLayoutType">
    <xsd:attribute name="locationRepresentation"
                   type="locationRepresentationType"
                   use="required"/>
  </xsd:complexType>

  <!-- Data type for location elements -->
  <xsd:complexType name="locationType">
    <xsd:sequence>
      <xsd:element name="link"
                   type="locationLinkType"
                   minOccurs="0"
                   maxOccurs="unbounded"/>
      <xsd:element name="property"
                   type="propertyType"
                   minOccurs="0"
                   maxOccurs="unbounded"/>
      <xsd:element name="locationLayout"
                   type="locationLayoutType"
                   minOccurs="0"
                   maxOccurs="1"/>
    </xsd:sequence>
    <xsd:attribute name="name"
                   type="xsd:string"
                   use="required"/>
    <xsd:attribute name="xPosition"
                   type="xsd:long"/>
    <xsd:attribute name="yPosition"
                   type="xsd:long"/>
    <xsd:attribute name="zPosition"
                   type="xsd:long"/>
    <xsd:attribute name="type"
                   type="xsd:string"
                   use="required"/>
    <xsd:attribute name="locked"
                   type="xsd:boolean"
                   use="required"/>
  </xsd:complexType>
  
  <!-- Data type for location link elements -->
  <xsd:complexType name="locationLinkType">
    <xsd:sequence>
      <xsd:element name="allowedOperation"
                   minOccurs="0"
                   maxOccurs="unbounded">
        <xsd:complexType>
          <xsd:attribute name="name"
                         type="xsd:string"
                         use="required"/>
        </xsd:complexType>
      </xsd:element>
    </xsd:sequence>
    <xsd:attribute name="point"
                   type="xsd:string"
                   use="required"/>
  </xsd:complexType>
  
  <!-- Data type for location layout type elements -->
  <xsd:complexType name="locationLayoutType">
    <xsd:attribute name="xPosition"
                   type="xsd:long"
                   use="required"/>
    <xsd:attribute name="yPosition"
                   type="xsd:long"
                   use="required"/>
    <xsd:attribute name="xLabelOffset"
                   type="xsd:long"
                   use="required"/>
    <xsd:attribute name="yLabelOffset"
                   type="xsd:long"
                   use="required"/>
    <xsd:attribute name="locationRepresentation"
                   type="locationRepresentationType"
                   use="required"/>
    <xsd:attribute name="layerId"
                   type="xsd:int"
                   use="required"/>
  </xsd:complexType>

  <xsd:simpleType name="locationRepresentationType">
    <xsd:restriction base="xsd:string">
      <xsd:enumeration value="NONE"/>
      <xsd:enumeration value="DEFAULT"/>
      <xsd:enumeration value="LOAD_TRANSFER_GENERIC"/>
      <xsd:enumeration value="LOAD_TRANSFER_ALT_1"/>
      <xsd:enumeration value="LOAD_TRANSFER_ALT_2"/>
      <xsd:enumeration value="LOAD_TRANSFER_ALT_3"/>
      <xsd:enumeration value="LOAD_TRANSFER_ALT_4"/>
      <xsd:enumeration value="LOAD_TRANSFER_ALT_5"/>
      <xsd:enumeration value="WORKING_GENERIC"/>
      <xsd:enumeration value="WORKING_ALT_1"/>
      <xsd:enumeration value="WORKING_ALT_2"/>
      <xsd:enumeration value="RECHARGE_GENERIC"/>
      <xsd:enumeration value="RECHARGE_ALT_1"/>
      <xsd:enumeration value="RECHARGE_ALT_2"/>
    </xsd:restriction>
  </xsd:simpleType>
  
  <!-- Data type for block elements -->
  <xsd:complexType name="blockType">
    <xsd:sequence>
      <xsd:element name="member"
                   minOccurs="0"
                   maxOccurs="unbounded">
        <xsd:complexType>
          <xsd:attribute name="name"
                         type="xsd:string"
                         use="required"/>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="property"
                   type="propertyType"
                   minOccurs="0"
                   maxOccurs="unbounded"/>
      <xsd:element name="blockLayout"
                   type="blockLayoutType"
                   minOccurs="0"
                   maxOccurs="1"/>
    </xsd:sequence>
    <xsd:attribute name="name"
                   type="xsd:string"
                   use="required"/>
    <xsd:attribute name="type"
                   type="blockTypeType"/>
  </xsd:complexType>
  
  <!-- Data type for block layout type elements -->
  <xsd:complexType name="blockLayoutType">
    <xsd:attribute name="color"
                   type="colorType"
                   use="required"/>
  </xsd:complexType>
  
  <xsd:simpleType name="colorType">
    <xsd:restriction base="xsd:string">
      <xsd:pattern value="#[\dA-F]{6}"/>
    </xsd:restriction>
  </xsd:simpleType>
  
  <!-- Data type for block type elements -->
  <xsd:simpleType name="blockTypeType">
    <xsd:restriction base="xsd:string">
      <xsd:enumeration value="SINGLE_VEHICLE_ONLY"/>
      <xsd:enumeration value="SAME_DIRECTION_ONLY"/>
    </xsd:restriction>
  </xsd:simpleType>  
  
  <!-- Data type for the visual layout -->
  <xsd:complexType name="visualLayoutType">
    <xsd:sequence>
      <xsd:element name="layer"
                   type="layerType"
                   minOccurs="1"
                   maxOccurs="unbounded"/>
      <xsd:element name="layerGroup"
                   type="layerGroupType"
                   minOccurs="1"
                   maxOccurs="unbounded"/>
      <xsd:element name="property"
                   type="propertyType"
                   minOccurs="0"
                   maxOccurs="unbounded"/>
    </xsd:sequence>
	
    <xsd:attribute name="name"
                   type="xsd:string"
                   use="required"/>
    <xsd:attribute name="scaleX"
                   type="xsd:float"
                   use="required"/>
    <xsd:attribute name="scaleY"
                   type="xsd:float"
                   use="required"/>
  </xsd:complexType>
  
  <xsd:complexType name="layerType">
    <xsd:attribute name="id"
                   type="xsd:int"
                   use="required"/>
    <xsd:attribute name="ordinal"
                   type="xsd:int"
                   use="required"/>
    <xsd:attribute name="visible"
                   type="xsd:boolean"
                   use="required"/>
    <xsd:attribute name="name"
                   type="xsd:string"
                   use="required"/>
    <xsd:attribute name="groupId"
                   type="xsd:int"
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值