目前需要決議 datastage 匯出的 xml 源檔案,聽說用 JAXBContext 可以直接把xml 決議成 object然后操作,網上找了個例子,list 外面必須要一個 wrapper,請問有沒有誰有過類似經驗? 能提供原始碼最好,謝謝。
Datastage 的原始碼大概如下:
<?xml version="1.0" encoding="UTF-8"?>
<DSExport>
<Header CharacterSet="CP1252" ExportingTool="IBM InfoSphere DataStage Export" ToolVersion="8" ServerName="IBM-DATASTAGE0912-01" ToolInstanceID="dstage1" Date="2020-07-23" Time="04.09.53" ServerVersion="9.1"/>
<Job Identifier="orig" DateModified="2020-07-23" TimeModified="03.41.19">
<Record Identifier="ROOT" Type="JobDefn" Readonly="0">
<Property Name="Name">orig</Property>
<Property Name="Description">hahaha</Property>
<Property Name="NextID">162</Property>
<Property Name="Container">V0</Property>
<Property Name="FullDescription" PreFormatted="1">* VC LOGS *
^1_1 07/28/08 13:41:08 Initial Version
</Property>
<Property Name="JobVersion">56.1.8</Property>
<Property Name="BeforeSubr"></Property>
<Property Name="AfterSubr"></Property>
<Property Name="ControlAfterSubr">0</Property>
<Collection Name="Parameters" Type="Parameters">
<SubRecord>
<Property Name="Name">$QMGR</Property>
<Property Name="Prompt">MQ Manager name (QMGR)</Property>
<Property Name="Default">$PROJDEF</Property>
<Property Name="ParamType">0</Property>
<Property Name="ParamLength">0</Property>
<Property Name="ParamScale">0</Property>
</SubRecord>
<SubRecord>
<Property Name="Name">QINP_NAME</Property>
<Property Name="Prompt">Input Queue Name</Property>
<Property Name="Default">EDW.SAP.TTABLE</Property>
<Property Name="ParamType">0</Property>
<Property Name="ParamLength">0</Property>
<Property Name="ParamScale">0</Property>
</SubRecord>
</Collection>
<Property Name="NULLIndicatorPosition">0</Property>
<Property Name="IsTemplate">0</Property>
<Property Name="NLSLocale">,,,,</Property>
</Record>
<Record Identifier="V0" Type="ContainerView" Readonly="0">
<Property Name="Name">Job</Property>
<Property Name="NextID">1</Property>
<Property Name="IsTopLevel">0</Property>
<Property Name="StageList">V0S0|V0S24|V0S33|...</Property>
<Property Name="StageXPos">31|934|706|...</Property>
<Property Name="StageYPos">373|373|373|...</Property>
<Property Name="StageTypes">CCustomStage|CTransformerStage|CCustomStage|...</Property>
</Record>
</Job>
<Job Identifier="orig1" DateModified="2020-07-23" TimeModified="03.41.19">
<Record Identifier="ROOT" Type="JobDefn" Readonly="0">
<Property Name="Name">orig</Property>
<Property Name="Description">hahaha</Property>
<Property Name="NextID">162</Property>
<Property Name="Container">V0</Property>
<Property Name="FullDescription" PreFormatted="1">* VC LOGS *
^1_1 07/28/08 13:41:08 Initial Version
</Property>
<Property Name="JobVersion">56.1.8</Property>
<Property Name="BeforeSubr"></Property>
<Property Name="AfterSubr"></Property>
<Property Name="ControlAfterSubr">0</Property>
<Collection Name="Parameters" Type="Parameters">
<SubRecord>
<Property Name="Name">$QMGR</Property>
<Property Name="Prompt">MQ Manager name (QMGR)</Property>
<Property Name="Default">$PROJDEF</Property>
<Property Name="ParamType">0</Property>
<Property Name="ParamLength">0</Property>
<Property Name="ParamScale">0</Property>
</SubRecord>
<SubRecord>
<Property Name="Name">QINP_NAME</Property>
<Property Name="Prompt">Input Queue Name</Property>
<Property Name="Default">EDW.SAP.TTABLE</Property>
<Property Name="ParamType">0</Property>
<Property Name="ParamLength">0</Property>
<Property Name="ParamScale">0</Property>
</SubRecord>
</Collection>
<Property Name="NULLIndicatorPosition">0</Property>
<Property Name="IsTemplate">0</Property>
<Property Name="NLSLocale">,,,,</Property>
</Record>
<Record Identifier="V0" Type="ContainerView" Readonly="0">
<Property Name="Name">Job</Property>
<Property Name="NextID">1</Property>
<Property Name="IsTopLevel">0</Property>
<Property Name="StageList">V0S0|V0S24|V0S33|...</Property>
<Property Name="StageXPos">31|934|706|...</Property>
<Property Name="StageYPos">373|373|373|...</Property>
<Property Name="StageTypes">CCustomStage|CTransformerStage|CCustomStage|...</Property>
</Record>
</Job>
</DSExport>
uj5u.com熱心網友回復:
你想用 JAXBContext 轉成Object那需要定義好幾個類,把里面的屬性一個個都定義好,并且寫好注解
最終要保證這個類的結構能和這個xml檔案對應上
可能會形成如下的一些類:
@XmlRootElement
public class BasicDistinctDef implements Serializable, Cloneable {
private static final long serialVersionUID = 1L;
@XmlAttribute
public String desc;
@XmlAttribute
public String name;
@XmlAttribute
public String id;
@XmlAttribute
public Boolean centNode = false;
@XmlElement(name = "Node")
public ArrayList<BasicNodeDef> nodes;
}
@XmlRootElement
@XmlType(propOrder = { "desc", "node", "ip4", "ip3", "ip2", "ip1", "name", "id" })
public class BasicNodeDef implements Serializable, Cloneable {
private static final long serialVersionUID = 1L;
@XmlAttribute
public String desc;
@XmlAttribute
public String node;
@XmlAttribute
public String ip2;
@XmlAttribute
public String ip1;
@XmlAttribute
public String name;
@XmlAttribute
public String id;
@XmlAttribute
public String ip3;
@XmlAttribute
public String ip4;
}
最后我勸你一句:不要用JAXBContext
因此這個玩意雖然是Java原生并且開放了API的
但是其在java 1.8以下的實作和1.8以上的實作不同,導致版本兼容時非常麻煩
而在java 1.9及以上版本中,JAXBContext直接被洗掉了
也就是說,這是一個已經被Java官方不惜犧牲他們一直引以為傲的“向下兼容”特性,也要放棄的實作
說明這個實作可能在他們看來非常失敗
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/31081.html
標籤:Java SE
上一篇:idea怎么訪問jsp檔案?
