有没有像golang这样的xml序列化器

Is there any xml serializer like golang

我使用 XML 作为配置文件,并希望将文件反序列化为记录 像这样:

TAddress = record
    City, State :string; 
end;
TAccount = record
    ID: string;
end;
type Accounts = record
    ID:  string;
    Account: array of TAccount;
end;
type Person = record
    Address:    TAddress;
    AccountsID: int ;
    Accounts:   Accounts;
end;

和 XML 这样的:

<person AccountsID="1">
    <Address City="aa" State="bb"></Address>
    <Accounts ID="dd">
        <Account>
            <ID>a1</ID>
        </Account>
        <Account>
            <ID>a2</ID>
        </Account>
        <Account>
            <ID>a3</ID>
        </Account>
    </Accounts>
</person>

在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    Address
    AccountsID int `xml:"AccountsID,attr"`
    Accounts   Accounts
}

我读了:What's a good way to serialize Delphi object tree to XML--using RTTI and not custom code?

TJvAppXMLFileStorage这么大,我就不测试了

OmniXML 和 NativeXml 仅支持 class 但记录,它会将所有字段解析为子字段。为什么我使用记录:我不想在每个 class

中释放对象

superobject superobjectxml修改代码即可使用,但bug太多

除了自己写轮子还有什么好的方法吗?

kbmMW 的 XML marshaller/unmarshaller 同时支持记录和对象,因此它应该能够处理您的情况。

您可以试用免费的 kbmMW 社区版

示例元素与属性:

[kbmMW_Root('address',[mwrfIncludeOnlyTagged])]
TMyAddress = class
public
   [kbmMW_Attribute('address1')]
   MyAddress1:string;

   [kbmMW_Attribute('address2')]
   MyAddress2:string;

   [kbmMW_Element('zipcode')]
   MyZip:string;

   constructor Create(AAddress1,AAddress2,AZip:string);
end;