Thema: Delphi XML Erstellen

Einzelnen Beitrag anzeigen

franktron

Registriert seit: 11. Nov 2003
Ort: Oldenburg
1.442 Beiträge
 
Delphi 10.2 Tokyo Enterprise
 
#1

XML Erstellen

  Alt 28. Mai 2019, 15:30
Wie kann ich so eine XML in Delphi erstellen

Code:
var menuData = [{
    id: "1",
    name: "Video Players",
    items: [{
        id: "1_1",
        name: "HD Video Player",
        price: 220,
        icon: "images/products/1.png"
    }, {
        id: "1_2",
        name: "SuperHD Video Player",
        icon: "images/products/2.png",
        price: 270
    }]
}, {
    id: "2",
    name: "Televisions",
    items: [{
        id: "2_1",
        name: "SuperLCD 42",
        icon: "images/products/7.png",
        price: 1200
    }, {
        id: "2_2",
        name: "SuperLED 42",
        icon: "images/products/5.png",
        price: 1450
    }, {
        id: "2_3",
        name: "SuperLED 50",
        icon: "images/products/4.png",
        price: 1600
    }, {
        id: "2_4",
        name: "SuperLCD 55 (Not available)",
        icon: "images/products/6.png",
        price: 1350,
        disabled: true
    }, {
        id: "2_5",
        name: "SuperLCD 70",
        icon: "images/products/9.png",
        price: 4000
    }]
}, {
    id: "3",
    name: "Monitors",
    items: [{
        id: "3_1",
        name: "19\"",
        items: [{
            id: "3_1_1",
            name: "DesktopLCD 19",
            icon: "images/products/10.png",
            price: 160
        }]
    }, {
        id: "3_2",
        name: "21\"",
        items: [{
            id: "3_2_1",
            name: "DesktopLCD 21",
            icon: "images/products/12.png",
            price: 170
        }, {
            id: "3_2_2",
            name: "DesktopLED 21",
            icon: "images/products/13.png",
            price: 175
        }]
    }]
}, {
    id: "4",
    name: "Projectors",
    items: [{
        id: "4_1",
        name: "Projector Plus",
        icon: "images/products/14.png",
        price: 550
    }, {
        id: "4_2",
        name: "Projector PlusHD",
        icon: "images/products/15.png",
        price: 750
    }]
}];
Mein Versuch sieht so aus

Delphi-Quellcode:
function TKAS_Modul_main.GetJSONMenu: string;
var
  Writer : TJsonTextWriter;
  StringWriter: TStringWriter;
begin
  StringWriter:=TStringWriter.Create();
  Writer:=TJsonTextWriter.Create(StringWriter);
  Writer.Formatting:=TJsonFormatting.Indented;
  Writer.WriteStartArray;
  GetMenuItems(Writer,KasMenu);
  Writer.WriteEndArray;
  Result:=StringWriter.ToString;
  Writer.Free;
  StringWriter.Free;
end;

procedure TKAS_Modul_main.GetMenuItems(var Writer: TJsonTextWriter;
  const MenuItem: TKASMenuItems);
var
  i : Longint;
begin
  For i:=0 To MenuItem.Count-1 do
  Begin
    Writer.WriteStartObject;
    Writer.WritePropertyName('name');
    Writer.WriteValue(MenuItem.Items[i].Name);

    Writer.WritePropertyName('url');
    Writer.WriteValue(MenuItem.Items[i].URL);

    Writer.WritePropertyName('icon');
    Writer.WriteValue(MenuItem.Items[i].Icon);

    Writer.WritePropertyName('tab');
    Writer.WriteValue(MenuItem.Items[i].isTab);
    Writer.WriteEndObject;
    if MenuItem.Items[i].SubItems.Count>0 then
    Begin
      Writer.WriteStartObject;
      Writer.WritePropertyName('items');
      Writer.WriteStartArray;
      GetMenuItems(Writer,MenuItem.Items[i].SubItems);
      Writer.WriteEndArray;
      Writer.WriteEndObject;
    End;
  End;
end;
Frank
Tux sein Lieblingsquellcode
While anzfische<TuxSatt do begin
Fisch:=TFisch.Create; Tux.EssenFisch(Fisch); Fisch.Free;inc(anzfische); end;
  Mit Zitat antworten Zitat