XML Data Binding - problem z generowanym kodem

0

Mam problem z generowanym kodem przez delphi. Najprawdopodobniej coś robię źle i najgorsze że nie wiem co :) Ale do rzeczy, poniżej kod wygenerowany w postaci pliku XML:

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<category>
	<id_parent format="isUnsignedInt"></id_parent>
	<level_depth read_only="true" format="isUnsignedInt" readOnly="true"></level_depth>
	<nb_products_recursive notFilterable="true" read_only="true" readOnly="true"></nb_products_recursive>
	<active required="true" format="isBool"></active>
	<id_shop_default format="isUnsignedId"></id_shop_default>
	<is_root_category format="isBool"></is_root_category>
	<position></position>
	<date_add format="isDate"></date_add>
	<date_upd format="isDate"></date_upd>
<associations>
<categories nodeType="category" api="categories">
	<category>
	<id></id>
	</category>
</categories>
<products nodeType="product" api="products">
	<product>
	<id></id>
	</product>
</products>
</associations>
</category>
</prestashop>

I teraz np właściwość Active która powinna być typu boolean otrzymuje klasę/interface.

{ IXMLActiveType }

  IXMLActiveType = interface(IXMLNode)
    ['{C8D59159-116B-461A-BCBD-6825E9895D32}']
    { Property Accessors }
    function Get_Required: UnicodeString;
    function Get_Format: UnicodeString;
    procedure Set_Required(Value: UnicodeString);
    procedure Set_Format(Value: UnicodeString);
    { Methods & Properties }
    property Required: UnicodeString read Get_Required write Set_Required;
    property Format: UnicodeString read Get_Format write Set_Format;
  end;

Ponadto po załadowaniu XML i odczytaniu takiej właściwości zmienna jest zawsze pusta. Poniżej wklejam jeszcze XML z odczytem już konkretnych danych

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<category>
	<id><![CDATA[1]]></id>
	<id_parent><![CDATA[0]]></id_parent>
	<level_depth><![CDATA[0]]></level_depth>
	<nb_products_recursive notFilterable="true"><![CDATA[19]]></nb_products_recursive>
	<active><![CDATA[1]]></active>
	<id_shop_default><![CDATA[1]]></id_shop_default>
	<is_root_category><![CDATA[0]]></is_root_category>
	<position><![CDATA[0]]></position>
	<date_add><![CDATA[2019-08-06 16:47:20]]></date_add>
	<date_upd><![CDATA[2019-08-06 16:47:20]]></date_upd>
<associations>
<categories nodeType="category" api="categories">
	<category xlink:href="http://www.zzz.pl/debug_ps17/api/categories/2">
	<id><![CDATA[2]]></id>
	</category>
</categories>
<products nodeType="product" api="products"/>
</associations>
</category>
</prestashop>

oraz te same dane w formie JSON

{
  "category":
  {
    "id":1,
    "id_parent":"0",
    "level_depth":"0",
    "nb_products_recursive":"19",
    "active":"1",
    "id_shop_default":"1",
    "is_root_category":"0",
    "position":"0",
    "date_add":"2019-08-06 16:47:20",
    "date_upd":"2019-08-06 16:47:20",
    "name":"Baza",
    "link_rewrite":"baza",
    "description":"",
    "meta_title":"",
    "meta_description":"",
    "meta_keywords":"",
    "associations":
    {
      "categories":
      [
                {
          "id":"2"
        }
      ]
    }
  }
}

Natomiast nie chcę korzystać z formatu JSON ponieważ prestashop nie przyjmuje danych w jego formacie a jedynie może je zwracać dlatego wolę mięć wszystko w XML.

0

unit do xml-a rozumiem że wygenerowałeś za pomocą XML Data Binding Wizard?

0
robertz68 napisał(a):

unit do xml-a rozumiem że wygenerowałeś za pomocą XML Data Binding Wizard?

Tak używam XML Data Binding Wizard z delphi. Poniżej plik wygenerowany ale kompletnie jest jakiś nielogiczny.

0

Napisałem od podstaw prostą klasę i procedure do ładowania danych z XML, działa poprawnie, rozpoznaje także CDATA. Może nie jest najszybsza ale nie zawiera ponad 700 linijek kodu jak plik wygenerowany w xml data binding który dodatkowo nie działa :P

unit xml_categories;

interface

uses ComObj, MSXML, RestConst, SysUtils;

type
  TXMLCategory = class
  private
    Fid: integer;
    Fid_parent: integer;
    Flevel_depth: integer;
    Fnb_products_recursive: integer;
    Factive: boolean;
    Fid_shop_default: integer;
    Fis_root_category: boolean;
    Fposition: integer;
    Fdate_add: TDateTime;
    Fdate_upd: TDateTime;
    Fname: string;
    Flink_rewrite: string;
    Fdescription: string;
    Fmeta_title: string;
    Fmeta_description: string;
    Fmeta_keywords: string;
  public
    constructor Create;
    destructor Destroy; override;
    procedure LoadXML(AResource: string);
    property id: integer read FiD;
    property id_parent: integer read Fid_parent;
    property level_depth: integer read Flevel_depth;
    property nb_products_recursive: integer read Fnb_products_recursive;
    property active: boolean read Factive;
    property id_shop_default: integer read Fid_shop_default;
    property is_root_category: boolean read Fis_root_category;
    property position: integer read Fposition;
    property date_add: tdatetime read Fdate_add;
    property date_upd: tdatetime read Fdate_upd;
    property name: string read Fname;
    property link_rewrite: string read Flink_rewrite;
    property description: string read Fdescription;
    property meta_title: string read Fmeta_title;
    property meta_description: string read Fmeta_description;
    property meta_keywords: string read Fmeta_keywords;
  end;

implementation

constructor TXMLCategory.Create;
begin
  inherited;
end;

destructor TXMLCategory.Destroy;
begin
  inherited;
end;

procedure TXMLCategory.LoadXML(AResource: string);
var
  xml: IXMLDOMDocument;
  node: IXMLDomNode;
  nodes_row, nodes_se: IXMLDomNodeList;
  i, j: Integer;
  url: string;
  ole: OleVariant;
begin
  xml := CreateOleObject('Microsoft.XMLDOM') as IXMLDOMDocument;
  xml.async := False;
  xml.loadXML(AResource);
  if xml.parseError.errorCode <> 0 then
    Exit;

  nodes_row := xml.selectNodes('/prestashop/category');
  for i := 0 to nodes_row.length - 1 do
  begin
    node := nodes_row.item[i];
    Fid := node.selectSingleNode('id').nodeTypedValue;
    Fid_parent := node.selectSingleNode('id_parent').nodeTypedValue;
    Flevel_depth := node.selectSingleNode('level_depth').nodeTypedValue;
    Fnb_products_recursive := node.selectSingleNode('nb_products_recursive').nodeTypedValue;
    Factive := node.selectSingleNode('active').nodeTypedValue;
    Fid_shop_default := node.selectSingleNode('id_shop_default').nodeTypedValue;
    Fis_root_category := node.selectSingleNode('is_root_category').nodeTypedValue;
    Fposition := node.selectSingleNode('position').nodeTypedValue;
    Fdate_add := StrToDateTime(node.selectSingleNode('date_add').nodeTypedValue,WebPS);
    Fdate_upd := StrToDateTime(node.selectSingleNode('date_upd').nodeTypedValue,WebPS);
    Fname := node.selectSingleNode('name').nodeTypedValue;
    Flink_rewrite := node.selectSingleNode('link_rewrite').nodeTypedValue;
    Fdescription := node.selectSingleNode('description').nodeTypedValue;
    Fmeta_title := node.selectSingleNode('meta_title').nodeTypedValue;
    Fmeta_description := node.selectSingleNode('meta_description').nodeTypedValue;
    Fmeta_keywords := node.selectSingleNode('meta_keywords').nodeTypedValue;
  end;

end;

end.
0

gdy tworzysz unit za pomocą XML Data Binding Wizard to masz możliwość określenia typu danych jakie znajdują się w pliku xml dla każdego elementu. Sam wizard nie radzi sobie z tym zbyt dobrze i prawe zawsze ustawia że element ma typ UnicodeString. Jest to bardzo bezpieczne w dalszym parsowaniu pliku xml.
Możesz też przecież później poprawić wygenerowany unit i zmienić typy danych.

0
robertz68 napisał(a):

gdy tworzysz unit za pomocą XML Data Binding Wizard to masz możliwość określenia typu danych jakie znajdują się w pliku xml dla każdego elementu. Sam wizard nie radzi sobie z tym zbyt dobrze i prawe zawsze ustawia że element ma typ UnicodeString. Jest to bardzo bezpieczne w dalszym parsowaniu pliku xml.
Możesz też przecież później poprawić wygenerowany unit i zmienić typy danych.

Tak wiem że mogę ręcznie wybrać różne typy, ale po wygenerowaniu jeszcze trzeba modyfikować kod aby obsługiwało CDATA. Dlatego unity piszę sam i wszystko działa jak należy.

1 użytkowników online, w tym zalogowanych: 0, gości: 1