كيف نتعامل مع xml في الدلفي؟ - khiro.alg - 03-04-2010
السلام عليكم
كيف نتعامل مع xml في الدلفي؟
كيف نتعامل مع xml في الدلفي؟ - AvimimuS - 03-04-2010
وعليكم السلام ورحمة الله وبركاته
توجد مقالة حول هدا الموضوع(بالفرنسية) لشخص يدعي ' Jobe'
مرفقة + مثال توضيحي
كيف نتعامل مع xml في الدلفي؟ - AvimimuS - 03-04-2010
هناك ايضا اداة Eeay Xml
إقتباس :The Easy XML toolkit provides you with all the necessary XML utilities. With Easy XML
you get a CodeGear and W3C DOM compliant XML parser that can handle ASCII, UniCode and UTF-8 XML documents. The DOM Documents are generated by our SAX parser. This SAX parser is compliant with the 1.1 SAX for Pascal release.
The Easy XML DOM document can be used using the CodeGear interfaces, but can alse be used in an "object" way. This makes working with XML a lot easier.
Easy XML also contains two Dataset serializers. One generic TDataset serializer and a very fast TADODataset serializer. Both these dataset serializers generate Midas compliant XML code. You can read the result directly into a clientdataset!
التحميل من الموقع الرسمي:
PHP كود : (examples and helpfile included) http://www.rapware.net/downloads/RAPWare_Trial_D7.zip http://www.rapware.net/downloads/RAPWare_Trial_D2005.zip http://www.rapware.net/downloads/RAPWare_Trial_D2006.zip http://www.rapware.net/downloads/RAPWare_Trial_D2007.zip http://www.rapware.net/downloads/RAPWare_Trial_D2009.zip http://www.rapware.net/downloads/RAPWare_Trial_D2010.zip
تحتوي علي امثلة رائعة
بعد التثبيت تجدها علي المسار :إقتباس :C:\Documents and Settings\Bille\Mes documents\RAPWare\Source
كيف نتعامل مع xml في الدلفي؟ - AL-MOB4RM3G - 04-04-2010
اولا, جزاكم الله خيرا اخي AvimimuS
فيما يخص ملفات ال xml, هل من شئ يميّزها عن غيرها ؟
ام انها تستخدم مثلها مثل ملفات ال ini مثلا ؟
بارك الله لكم
كيف نتعامل مع xml في الدلفي؟ - kachwahed - 04-04-2010
وعليكم السلام ورحمة الله
أهلا بعودة AL-MOB4RM3G
إقتباس :كيف نتعامل مع xml في الدلفي؟
يتم التعامل مع XML من خلال مكون TXMLDocument المرفق مع إصدارات دلفي 6 من التبويب Internet
حيث الصيغة النحوية لملفات XML شبيه بالـ HTML ، بإمكانية تخزين عناصر وأبنائها بهذا الشكل:
PHP كود : XMLDocument1.Version := '1.0'; XMLDocument1.Encoding := 'ISO-8859-1'; with XMLDocument1.AddChild('Parent') do begin Text := 'father'; AddChild('Child').Text := 'first son'; AddChild('Child2').Text := 'Second son'; end; XMLDocument1.SaveToFile(FileName);
إقتباس :فيما يخص ملفات ال xml, هل من شئ يميّزها عن غيرها ؟
XML تستخدم محلل (Parser) خاص لقراءة/كتابة البنود
إقتباس :ام انها تستخدم مثلها مثل ملفات ال ini مثلا ؟
أبعد من ذلك... يمكن استخدامها لتخزين/قراءة بيانات جدول (TDataSet)، مثال:
PHP كود : procedure DatasetToXML(Dataset: TDataset; FileName: string);
{ The first Dataset parameter is source dataset with data (your Table or Query component, or some other third-party dataset). The second FileName parameter is a name of target XML-file. }
uses Classes, DB;
procedure DatasetToXML(Dataset: TDataset; FileName: string);
implementation
uses SysUtils;
var SourceBuffer: PChar;
procedure WriteString(Stream: TFileStream; s: string); begin StrPCopy(SourceBuffer, s); Stream.Write(SourceBuffer[0], StrLen(SourceBuffer)); end;
procedure WriteFileBegin(Stream: TFileStream; Dataset: TDataset);
function XMLFieldType(fld: TField): string; begin case fld.DataType of ftString: Result := '"string" WIDTH="' + IntToStr(fld.Size) + '"'; ftSmallint: Result := '"i4"'; //?? ftInteger: Result := '"i4"'; ftWord: Result := '"i4"'; //?? ftBoolean: Result := '"boolean"'; ftAutoInc: Result := '"i4" SUBTYPE="Autoinc"'; ftFloat: Result := '"r8"'; ftCurrency: Result := '"r8" SUBTYPE="Money"'; ftBCD: Result := '"r8"'; //?? ftDate: Result := '"date"'; ftTime: Result := '"time"'; //?? ftDateTime: Result := '"datetime"'; else end; if fld.Required then Result := Result + ' required="true"'; if fld.Readonly then Result := Result + ' readonly="true"'; end;
var i: Integer; begin WriteString(Stream, '<?xml version="1.0" standalone="yes"?><!-- Generated by SMExport --> ' + '<DATAPACKET Version="2.0">'); WriteString(Stream, '<METADATA><FIELDS>');
{write th metadata} with Dataset do for i := 0 to FieldCount-1 do begin WriteString(Stream, '<FIELD attrname="' + Fields[i].FieldName + '" fieldtype=' + XMLFieldType(Fields[i]) + '/>'); end; WriteString(Stream, '</FIELDS>'); WriteString(Stream, '<PARAMS DEFAULT_ORDER="1" PRIMARY_KEY="1" LCID="1033"/>'); WriteString(Stream, '</METADATA><ROWDATA>'); end;
procedure WriteFileEnd(Stream: TFileStream); begin WriteString(Stream, '</ROWDATA></DATAPACKET>'); end;
procedure WriteRowStart(Stream: TFileStream; IsAddedTitle: Boolean); begin if not IsAddedTitle then WriteString(Stream, '<ROW'); end;
procedure WriteRowEnd(Stream: TFileStream; IsAddedTitle: Boolean); begin if not IsAddedTitle then WriteString(Stream, '/>'); end;
procedure WriteData(Stream: TFileStream; fld: TField; AString: ShortString); begin if Assigned(fld) and (AString <> '') then WriteString(Stream, ' ' + fld.FieldName + '="' + AString + '"'); end;
function GetFieldStr(Field: TField): string;
function GetDig(i, j: Word): string; begin Result := IntToStr(i); while (Length(Result) < j) do Result := '0' + Result; end;
var Hour, Min, Sec, MSec: Word; begin case Field.DataType of ftBoolean: Result := UpperCase(Field.AsString); ftDate: Result := FormatDateTime('yyyymmdd', Field.AsDateTime); ftTime: Result := FormatDateTime('hhnnss', Field.AsDateTime); ftDateTime: begin Result := FormatDateTime('yyyymmdd', Field.AsDateTime); DecodeTime(Field.AsDateTime, Hour, Min, Sec, MSec); if (Hour <> 0) or (Min <> 0) or (Sec <> 0) or (MSec <> 0) then Result := Result + 'T' + GetDig(Hour, 2) + ':' + GetDig(Min, 2) + ':' + GetDig(Sec, 2) + GetDig(MSec, 3); end; else Result := Field.AsString; end; end;
procedure DatasetToXML(Dataset: TDataset; FileName: string); var Stream: TFileStream; bkmark: TBookmark; i: Integer; begin Stream := TFileStream.Create(FileName, fmCreate); SourceBuffer := StrAlloc(1024); WriteFileBegin(Stream, Dataset);
with DataSet do begin DisableControls; bkmark := GetBookmark; First;
{write a title row} WriteRowStart(Stream, True); for i := 0 to FieldCount-1 do WriteData(Stream, nil, Fields[i].DisplayLabel); {write the end of row} WriteRowEnd(Stream, True);
while (not EOF) do begin WriteRowStart(Stream, False); for i := 0 to FieldCount-1 do WriteData(Stream, Fields[i], GetFieldStr(Fields[i])); {write the end of row} WriteRowEnd(Stream, False);
Next; end;
GotoBookmark(bkmark); EnableControls; end;
WriteFileEnd(Stream); Stream.Free; StrDispose(SourceBuffer); end;
end.
كما يمكن استخدامها عوضا عن ملفات INI، مثال:
PHP كود : uses Forms, SysUtils, Windows, XmlIntf, XMLDoc;
type TXMLConfig = class private FModified: Boolean; FFileName: string; FXMLDoc: TXMLDocument; FBackup: Boolean; function GetVersion: string; public constructor Create(const FileName: string); overload; constructor Create; overload; destructor Destroy; override; procedure Save; function ReadString(const Section, Key, default: string): string; procedure WriteString(const Section, Key, Value: string); function ReadInteger(const Section, Key: string; default: Integer): Integer; procedure WriteInteger(const Section, Key: string; Value: Integer); function ReadBoolean(const Section, Key: string; default: Boolean): Boolean; procedure WriteBoolean(const Section, Key: string; Value: Boolean); property Backup: Boolean read FBackup write FBackup; property Version: string read GetVersion; end;
implementation
{ TXMLConfig }
constructor TXMLConfig.Create(const FileName: string); begin inherited Create; FBackup := True; FFileName := FileName; FXMLDoc := TXMLDocument.Create(Application); FXMLDoc.Options := [doNodeAutoIndent]; if FileExists(FFileName) then FXMLDoc.LoadFromFile(FFileName) else begin FXMLDoc.Active := True; FXMLDoc.AddChild('Configuration'); end; end;
constructor TXMLConfig.Create; begin Create(ChangeFileExt(Application.Exename, '_cfg.xml')); end;
destructor TXMLConfig.Destroy; begin Save; FXMLDoc.Destroy; inherited; end;
function TXMLConfig.GetVersion: string; begin Result := '1.00'; end;
function TXMLConfig.ReadBoolean(const Section, Key: string; default: Boolean): Boolean; begin Result := Boolean(ReadInteger(Section, Key, Integer(default))); end;
function TXMLConfig.ReadInteger(const Section, Key: string; default: Integer): Integer; begin Result := StrToInt(ReadString(Section, Key, IntToStr(default))); end;
function TXMLConfig.ReadString(const Section, Key, default: string): string; var Node: IXMLNode; begin Node := FXMLDoc.DocumentElement.ChildNodes.FindNode(Section); if Assigned(Node) and Node.HasAttribute(Key) then Result := Node.Attributes[Key] else Result := default; end;
procedure TXMLConfig.Save; begin if not FModified then Exit; if FBackup then
CopyFile(PChar(FFileName), PChar(FFileName + '.bak'), False); FXMLDoc.SaveToFile(FFileName); FModified := False; end;
procedure TXMLConfig.WriteBoolean(const Section, Key: string; Value: Boolean); begin WriteInteger(Section, Key, Integer(Value)); end;
procedure TXMLConfig.WriteInteger(const Section, Key: string; Value: Integer); begin WriteString(Section, Key, IntToStr(Value)); end;
procedure TXMLConfig.WriteString(const Section, Key, Value: string); var Node: IXMLNode; begin if ReadString(Section, Key, '') = Value then Exit; Node := FXMLDoc.DocumentElement.ChildNodes.FindNode(Section); if not Assigned(Node) then Node := FXMLDoc.DocumentElement.AddChild(Section); Node.Attributes[Key] := Value; FModified := True; end;
كيف نتعامل مع xml في الدلفي؟ - AL-MOB4RM3G - 04-04-2010
جميل, جوآب شافي ووآضح, جزاك الله خيرا اخي كاش واحد,
اظن هذا الموضوع الأول في المنتدى عن ملفات ال xml ؟ على كل حال, البحث لا يعطي اية نتيجة,
إقتباس :أهلا بعودة al-mob4rm3g
الله يبارك فيكم اخي, سأكون متوآجد اكثر الآن ان شاء الله,
بالتوفيق اخوآني,
كيف نتعامل مع xml في الدلفي؟ - shagrouni - 04-04-2010
السلام عليكم
وضعت مثالا عن التعامل مع XML في قسم التعامل مع الملفات.
توجد أيضا صيغة قياسية أخرى لحفظ وتبادل البيانات أخذت في الانتشار في المدة الأخيرة،
وهي صيغة JSON كبديل أسهل وأقل تعقيد من XML. دلفي 2010 تدعم هذه الصيغة.
الموقع:
كود : http://json.org/json-ar.html
يقدم لمحة عن JSON بالعربية، و به إشارة لبعض المشاريح المفتوحة للتعامل معل ملفات
JSON في دلفي.
كيف نتعامل مع xml في الدلفي؟ - khiro.alg - 11-07-2010
بارك الله فيكم إخوان
لدي إقتراح لمن ه متمكن من التعامل مع مفات الـxml ولديه بض الوقت لإفادة إخوانه
- وهو وضع موضوع مفصل بقدر ما يستطيع المتطوع يتكلم عن الـxml وطق التعامل معه
لأن في المنتدى كاملا يوجد فقط متال واحد بسيط مبهم للمبتدئين .
جزاكم الله كل خير
|