Автор: Cryogen2003
Дата сообщения: 10.02.2011 10:42
Переписал этот вариант с си шарпа, вроде работает )))))
Если надо, то пользуйтесь
[more=uDeSerialize.pas]
Unit
uDeSerialize;
Interface
Type
TDeSerialize = Class(TObject)
Private
FXML: String;
FDSS: String;
StrPos: Integer;
Procedure SetXML(Const Value: String);
Procedure SetDSS(Const Value: String);
Procedure DoDeSerialize(IsArr: Boolean = False);
procedure WriteBeginXML;
procedure WriteEndXML;
Public
Constructor Create;
Destructor Destroy; Override;
Procedure DeSerialize;
Published
Property XML: String Read FXML Write SetXML;
Property DeSerializeStr: String Read FDSS Write SetDSS;
End;
Implementation
Uses
CEBUtils,
DevExpressCEB,
SysUtils,
StringUtils;
{ TDeSerialize }
Const
strNull = 'NULL';
strBool = 'BOOLEAN';
strInt = 'INTEGER';
strDouble = 'DOUBLE';
strString = 'STRING';
strArrayBegin = '<ARRAY>';
strArrayEnd = '</ARRAY>';
strUnknown = 'UNKNOWN';
Constructor TDeSerialize.Create;
Begin
StrPos := 1;
End;
Procedure TDeSerialize.WriteBeginXML;
Begin
FXML := '<?xml version="1.0" encoding="windows-1251"?>' +
'<!-- Generated by TDeSerialize --><!-- Created by Cryogen -->' +
'<DATA Version="1.0">';
End;
Procedure TDeSerialize.WriteEndXML;
Begin
FXML := FXML + '</DATA>';
End;
Procedure TDeSerialize.DeSerialize;
Begin
If IsClearText(FDSS) Then
Exit;
XML := EmptyStr;
StrPos := 1;
WriteBeginXML;
DoDeSerialize;
WriteEndXML;
End;
Destructor TDeSerialize.Destroy;
Begin
Inherited;
End;
Procedure TDeSerialize.DoDeSerialize(IsArr: Boolean = False);
Var
StrStart: Integer;
StrEnd: Integer;
chBool: Char;
stInt: String;
stDouble: String;
stLen: String;
ByteLen: Integer;
stLength: Integer;
stRet: String;
I: Integer;
Z: Integer;
Key: String;
Def: Integer;
TLen: Integer;
Begin
Case FDSS[StrPos] Of
'N':
Begin
Inc(StrPos, 2);
If Not IsArr Then
FXML := FXML + '<' + strNull + '></' + strNull + '<'
Else
FXML := FXML + strNull;
End;
'b':
Begin
chBool := FDSS[StrPos + 2];
Inc(StrPos, 4);
If Not IsArr Then
If chBool = '1' Then
FXML := FXML + '<' + strBool + '>TRUE</' + strBool + '<'
Else
FXML := FXML + '<' + strBool + '>FALSE</' + strBool + '<'
Else
If chBool = '1' Then
FXML := FXML + 'TRUE'
Else
FXML := FXML + 'FALSE';
End;
'i':
Begin
StrStart := QuickPosText(':', FDSS, StrPos) + 1;
StrEnd := QuickPosText(';', FDSS, StrStart);
stInt := Copy(FDSS, StrStart, StrEnd - StrStart);
Inc(StrPos, 3 + Length(stInt));
If Not IsArr Then
FXML := FXML + '<' + strInt + '>' + IntToStr(MyStrToInt(stInt)) +
'</' + strInt + '<'
Else
FXML := FXML + IntToStr(MyStrToInt(stInt));
End;
'd':
Begin
StrStart := QuickPosText(':', FDSS, StrPos) + 1;
StrEnd := QuickPosText(';', FDSS, StrStart);
stDouble := Copy(FDSS, StrStart, StrEnd - StrStart);
Inc(StrPos, 3 + Length(stDouble));
If Not IsArr Then
FXML := FXML + '<' + strDouble + '>' +
FloatToStr(MyStrToFloat(stDouble)) + '</' + strDouble + '<'
Else
FXML := FXML + FloatToStr(MyStrToFloat(stDouble));
End;
's':
Begin
StrStart := QuickPosText(':', FDSS, StrPos) + 1;
StrEnd := QuickPosText(':', FDSS, StrStart);
stLen := Copy(FDSS, StrStart, StrEnd - StrStart);
ByteLen := MyStrToInt(stLen);
stLength := ByteLen;
If StrEnd + 2 + stLength >= Length(FDSS) Then
stLength := Length(FDSS) - 2 - StrEnd;
stRet := Copy(FDSS, StrEnd + 2, stLength);
If QuickPosText('"', stRet) > 0 Then
Begin
stRet := Copy(stRet, 1, QuickPosText('"', stRet) - 1);
stLength := Length(stRet);
End;
Inc(StrPos, 6 + Length(stLen) + stLength);
stRet := QuickReplaceText(QuickReplaceText(QuickClearText(stRet),
#10, ''), #13, '');
If Not IsArr Then
FXML := FXML + '<' + strString + '>' + stRet + '</' + strString + '<'
Else
FXML := FXML + stRet;
End;
'a':
Begin
StrStart := QuickPosText(':', FDSS, StrPos) + 1;
StrEnd := QuickPosText(':', FDSS, StrStart);
stLen := Copy(FDSS, StrStart, StrEnd - StrStart);
stLength := MyStrToInt(stLen);
FXML := FXML + strArrayBegin;
Inc(StrPos, 4 + Length(stLen));
For I := 0 To Pred(stLength) Do
Begin
//key
FXML := FXML + '<';
DoDeSerialize(True);
Key := EmptyStr;
For Z := Length(FXML) DownTo 1 Do
If FXML[Z] <> '<' Then
Key := FXML[Z] + Key
Else
Break;
If IsClearText(Key) Then
Begin
Key := 'INDX';
FXML := FXML + Key;
End
Else
Begin
Def := StrToIntDef(Key, -779977);
If Def <> -779977 Then
Begin
TLen := Length(FXML) - Length(Key);
SetLength(FXML, TLen);
Key := 'INDX' + Key;
FXML := FXML + Key;
End;
End;
FXML := FXML + '>';
//value
DoDeSerialize(True);
FXML := FXML + '</' + Key + '>';
End;
Inc(StrPos);
If (StrPos < Length(FDSS)) And (FDSS[StrPos] = ';') Then
Inc(StrPos);
FXML := FXML + strArrayEnd;
End;
Else
Begin
If Not IsArr Then
FXML := FXML + '<' + strUnknown + '></' + strUnknown + '<'
Else
FXML := FXML + strUnknown;
End;
End;
End;
Procedure TDeSerialize.SetDSS(Const Value: String);
Begin
FDSS := Value;
End;
Procedure TDeSerialize.SetXML(Const Value: String);
Begin
FXML := Value;
End;
End.
[/more]