我使用XML作為組態檔,并希望將該檔案反序列化為一條記錄。 像這樣:
TAddress = record
城市,州:字串。
end;
TAccount = record; record
ID: string;
end;
type Accounts = record
ID: string;
帳戶。array of TAccount;
end;
type Person = record
地址。 TAddress;
AccountsID: int ;
帳戶。 帳戶。
end。
而XML是這樣的:
<person AccountsID="1"/span>>
<Address City="aa"/span> State="bb"/span>> </Address>>
<Accounts ID="dd">/span>
<Account>/span>
<ID>/span>a1</ID>/span>
</Account>/span>
<Account>/span>
<ID>/span>a2</ID>/span>
</Account>/span>
<Account>/span>
<ID>/span>a3</ID>/span>
</Account>/span>
</Accounts>/span>
</person>/span>
在golang中,控制一個欄位作為一個屬性或者作為一個標簽的子節點是很簡單的:
type Address struct {
City, State string `xml: ",attr"`
}
type Account struct {
ID string `xml: ",attr"`
}
type Accounts struct {
ID string `xml: ",attr"`
Account []Account"。
}
type Person struct {
XMLName xml.Name `xml: "person"`
Address AddressAccountsID int `xml:"AccountsID, attr"`
Accounts Accounts Accounts
}
我讀到:有什么好的方法可以將Delphi物件樹序列化為XML--使用RTTI而不是自定義代碼?
TJvAppXML是一個很好的例子。
TJvAppXMLFileStorage太大,我沒有測驗它
。OmniXML和NativeXml只支持類而不支持記錄,而且它把所有的欄位都決議為子項。為什么我使用記錄。我不想在每個類中都釋放物件
超級物件 superobject
superobject superobjectxml可以通過修改其代碼來使用,但是有太多的bug。
除了寫我自己的輪子,還有什么好辦法嗎?
uj5u.com熱心網友回復:
kbmMW的XML marshaller/unmarshaller同時支持記錄和物件,所以它應該能夠處理你的情況。
你可以試試kbmMW社區版,它是免費的
。示例元素與屬性:
[kbmMW_Root('address',[mwrfIncludeOnlyTagged]) ]
TMyAddress = 類
public
[kbmMW_Attribute('address1') ]
MyAddress1:string;
[kbmMW_Attribute('address2')]
MyAddress2:string;
[kbmMW_Element('zipcode') ]
MyZip:string;
constructor Create(Address1,Address2,AZip:string); /span>
end。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/316515.html
標籤:
