Home > Enterprise >  Is there any xml serializer like golang
Is there any xml serializer like golang

Time:09-17

I use the XML as config file, and want to deserialize the file as a record like this:

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;

and the XML like this:

<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>

in golang it is simple to control a field As an attribute or as a child node by tag:

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
}

I read it : What's a good way to serialize Delphi object tree to XML--using RTTI and not custom code?

TJvAppXMLFileStorage is so large, I don't test it

OmniXML and NativeXml only support class but record, and it parses all fields as children. Why I use record: I don't want to free object in every class

superobject superobjectxml can uses by modify its code, but there are too many bugs.

Is there any good way but write my own wheel?

CodePudding user response:

kbmMW's XML marshaller/unmarshaller supports both records and objects, so it should be able to handle your case.

You can try out kbmMW Community Edition which is free

Example elements vs attributes:

[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;
  • Related