Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   XML (https://www.delphipraxis.net/46-xml/)
-   -   Delphi XML Erstellen (https://www.delphipraxis.net/200813-xml-erstellen.html)

franktron 28. Mai 2019 15:30

XML Erstellen
 
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;

TiGŁ 28. Mai 2019 15:54

AW: XML Erstellen
 
Kein XML, das ist JSON, wie du ja laut Quelltext schon richtig erkannt hast.

Was kommt denn bei deinen Versuch bei rum? Das fehlt jetzt ein wenig.
Also wie sieht dein bisheriges Ergebnis im Vergleich zum Gegeben aus?


Alle Zeitangaben in WEZ +2. Es ist jetzt 09:32 Uhr.

Powered by vBulletin® Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2021 by Daniel R. Wolf