📄 jvqsimplexml.pas
字号:
finally
if DoFree then
AOutStream.Free;
end;
end;
procedure TJvSimpleXML.LoadFromString(const Value: string);
var
Stream: TStringStream;
begin
Stream := TStringStream.Create(Value);
try
LoadFromStream(Stream);
finally
Stream.Free;
end;
end;
procedure TJvSimpleXML.SaveToFile(FileName: TFileName);
var
Stream: TFileStream;
begin
if FileExists(FileName) then
begin
Stream := TFileStream.Create(FileName, fmOpenWrite);
Stream.Size := 0;
end
else
Stream := TFileStream.Create(FileName, fmCreate);
try
SaveToStream(Stream);
finally
Stream.Free;
end;
end;
procedure TJvSimpleXML.SaveToStream(Stream: TStream);
var
lCount: Integer;
AOutStream: TStream;
DoFree: Boolean;
begin
if Assigned(FOnEncodeStream) then
begin
AOutStream := TMemoryStream.Create;
DoFree := True;
end
else
begin
AOutStream := Stream;
DoFree := False;
end;
try
if Assigned(FOnSaveProg) then
begin
lCount := Root.ChildsCount + Prolog.Count;
FSaveCount := lCount;
FSaveCurrent := 0;
FOnSaveProg(Self, 0, lCount);
Prolog.SaveToStream(AOutStream, Self);
Root.SaveToStream(AOutStream, '', Self);
FOnSaveProg(Self, lCount, lCount);
end
else
begin
Prolog.SaveToStream(AOutStream);
Root.SaveToStream(AOutStream);
end;
if Assigned(FOnEncodeStream) then
begin
AOutStream.Seek(0, soFromBeginning);
FOnEncodeStream(Self, AOutStream, Stream);
end;
finally
if DoFree then
AOutStream.Free;
end;
end;
function TJvSimpleXML.SaveToString: string;
var
Stream: TStringStream;
begin
Stream := TStringStream.Create('');
try
SaveToStream(Stream);
Result := Stream.DataString;
finally
Stream.Free;
end;
end;
procedure TJvSimpleXML.SetFileName(Value: TFileName);
begin
FFileName := Value;
LoadFromFile(Value);
end;
//=== { TJvSimpleXMLElem } ===================================================
procedure TJvSimpleXMLElem.Assign(Value: TJvSimpleXMLElem);
var
Elems: TJvSimpleXMLElem;
Elem: TJvSimpleXMLElem;
I: Integer;
begin
Clear;
if Value = nil then
Exit;
Elems := TJvSimpleXMLElem(Value);
Name := Elems.Name;
Self.Value := Elems.Value;
for I := 0 to Elems.Properties.Count - 1 do
Properties.Add(Elems.Properties[I].Name, Elems.Properties[I].Value);
for I := 0 to Elems.Items.Count - 1 do
begin
Elem := Items.Add(Elems.Items[I].Name, Elems.Items[I].Value);
Elem.Assign(TJvSimpleXMLElem(Elems.Items[I]));
end;
end;
procedure TJvSimpleXMLElem.Clear;
begin
if FItems <> nil then
FItems.Clear;
if FProps <> nil then
FProps.Clear;
end;
constructor TJvSimpleXMLElem.Create(const AOwner: TJvSimpleXMLElem);
begin
inherited Create;
FName := '';
FParent := TJvSimpleXMLElem(AOwner);
FContainer := nil;
end;
destructor TJvSimpleXMLElem.Destroy;
begin
FParent := nil;
Clear;
FreeAndNil(FItems);
FreeAndNil(FProps);
inherited Destroy;
end;
procedure TJvSimpleXMLElem.Error(const S: string);
begin
raise EJvSimpleXMLError.Create(S);
end;
procedure TJvSimpleXMLElem.FmtError(const S: string;
const Args: array of const);
begin
Error(Format(S, Args));
end;
procedure TJvSimpleXMLElem.GetBinaryValue(const Stream: TStream);
var
I, J: Integer;
St: string;
Buf: array [0..cBufferSize - 1] of Byte;
begin
I := 1;
J := 0;
while I < Length(Value) do
begin
St := '$' + Value[I] + Value[I + 1];
if J = cBufferSize - 1 then //Buffered write to speed up the process a little
begin
Stream.Write(Buf, J);
J := 0;
end;
Buf[J] := StrToIntDef(St, 0);
Inc(J);
Inc(I, 2);
end;
Stream.Write(Buf, J);
end;
function TJvSimpleXMLElem.GetBoolValue: Boolean;
begin
Result := StrToBoolDef(Value, False);
end;
function TJvSimpleXMLElem.GetChildIndex(
const AChild: TJvSimpleXMLElem): Integer;
begin
if FItems = nil then
Result := -1
else
Result := FItems.FElems.IndexOfObject(AChild);
end;
function TJvSimpleXMLElem.GetChildsCount: Integer;
var
I: Integer;
begin
Result := 1;
if FItems <> nil then
for I := 0 to FItems.Count - 1 do
Result := Result + FItems[I].ChildsCount;
end;
function TJvSimpleXMLElem.GetFloatValue: Extended;
begin
Result := StrToFloatDef(Value, 0.0);
end;
function TJvSimpleXMLElem.GetIntValue: Int64;
begin
Result := StrToInt64Def(Value, -1);
end;
function TJvSimpleXMLElem.GetItems: TJvSimpleXMLElems;
begin
if FItems = nil then
FItems := TJvSimpleXMLElems.Create(Self);
Result := FItems;
end;
function TJvSimpleXMLElem.GetProps: TJvSimpleXMLProps;
begin
if FProps = nil then
FProps := TJvSimpleXMLProps.Create(Self);
Result := FProps;
end;
function TJvSimpleXMLElem.GetSimpleXML: TJvSimpleXML;
begin
if FParent <> nil then
Result := FParent.GetSimpleXML
else
Result := FSimpleXML;
end;
procedure TJvSimpleXMLElem.LoadFromString(const Value: string);
var
Stream: TStringStream;
begin
Stream := TStringStream.Create(Value);
try
LoadFromStream(Stream);
finally
Stream.Free;
end;
end;
function TJvSimpleXMLElem.SaveToString: string;
var
Stream: TStringStream;
begin
Stream := TStringStream.Create('');
try
SaveToStream(Stream);
Result := Stream.DataString;
finally
Stream.Free;
end;
end;
procedure TJvSimpleXMLElem.SetBoolValue(const Value: Boolean);
begin
FValue := BoolToStr(Value);
end;
procedure TJvSimpleXMLElem.SetFloatValue(const Value: Extended);
begin
FValue := FloatToStr(Value);
end;
procedure TJvSimpleXMLElem.SetIntValue(const Value: Int64);
begin
FValue := IntToStr(Value);
end;
procedure TJvSimpleXMLElem.SetName(const Value: string);
begin
if (Value <> FName) and (Value <> '') then
begin
if (Parent <> nil) and (FName <> '') then
Parent.Items.DoItemRename(Self, Value);
FName := Value;
end;
end;
//=== { TJvSimpleXMLElems } ==================================================
function TJvSimpleXMLElems.Add(const Name: string): TJvSimpleXMLElemClassic;
begin
Result := TJvSimpleXMLElemClassic.Create(Parent);
Result.FName := Name; //Directly set parent to avoid notification
AddChild(Result);
end;
function TJvSimpleXMLElems.Add(const Name, Value: string): TJvSimpleXMLElemClassic;
begin
Result := TJvSimpleXMLElemClassic.Create(Parent);
Result.Name := Name;
Result.Value := Value;
AddChild(Result);
end;
function TJvSimpleXMLElems.Add(const Name: string; const Value: Int64): TJvSimpleXMLElemClassic;
begin
Result := Add(Name, IntToStr(Value));
end;
function TJvSimpleXMLElems.Add(Value: TJvSimpleXMLElem): TJvSimpleXMLElem;
begin
if Value <> nil then
AddChild(Value);
Result := Value;
end;
function TJvSimpleXMLElems.Add(const Name: string;
const Value: Boolean): TJvSimpleXMLElemClassic;
begin
Result := Add(Name, BoolToStr(Value));
end;
function TJvSimpleXMLElems.Add(const Name: string;
const Value: TStream): TJvSimpleXMLElemClassic;
var
Stream: TStringStream;
Buf: array [0..cBufferSize - 1] of Byte;
St: string;
I, Count: Integer;
begin
Stream := TStringStream.Create('');
repeat
Count := Value.Read(Buf, SizeOf(Buf));
St := '';
for I := 0 to Count - 1 do
St := St + IntToHex(Buf[I], 2);
Stream.WriteString(St);
until Count = 0;
Result := Add(Name, Stream.DataString);
Stream.Free;
end;
procedure TJvSimpleXMLElems.AddChild(const Value: TJvSimpleXMLElem);
begin
CreateElems;
// If there already is a container, notify it to remove the element
if Assigned(Value.Container) then
Value.Container.Notify(Value, opRemove);
FElems.AddObject(Value.Name, Value);
Notify(Value, opInsert);
end;
procedure TJvSimpleXMLElems.AddChildFirst(const Value: TJvSimpleXMLElem);
begin
CreateElems;
// If there already is a container, notify it to remove the element
if Assigned(Value.Container) then
Value.Container.Notify(Value, opRemove);
FElems.InsertObject(0, Value.Name, Value);
Notify(Value, opInsert);
end;
function TJvSimpleXMLElems.AddFirst(const Name: string): TJvSimpleXMLElemClassic;
begin
Result := TJvSimpleXMLElemClassic.Create(Parent);
Result.FName := Name; //Directly set parent to avoid notification
AddChildFirst(Result);
end;
function TJvSimpleXMLElems.AddFirst(Value: TJvSimpleXMLElem): TJvSimpleXMLElem;
begin
if Value <> nil then
AddChildFirst(Value);
Result := Value;
end;
function TJvSimpleXMLElems.AddComment(const Name,
Value: string): TJvSimpleXMLElemComment;
begin
Result := TJvSimpleXMLElemComment.Create(Parent);
Result.FName := Name;
Result.Value := Value;
AddChild(Result);
end;
function TJvSimpleXMLElems.AddCData(const Name, Value: string): TJvSimpleXMLElemCData;
begin
Result := TJvSimpleXMLElemCData.Create(Parent);
Result.FName := Name;
Result.Value := Value;
AddChild(Result);
end;
function TJvSimpleXMLElems.AddText(const Name, Value: string): TJvSimpleXMLElemText;
begin
Result := TJvSimpleXMLElemText.Create(Parent);
Result.FName := Name;
Result.Value := Value;
AddChild(Result);
end;
procedure TJvSimpleXMLElems.BinaryValue(const Name: string;
const Stream: TStream);
var
Elem: TJvSimpleXMLElem;
begin
Elem := GetItemNamed(Name);
if Elem <> nil then
Elem.GetBinaryValue(Stream);
end;
function TJvSimpleXMLElems.BoolValue(const Name: string; Default: Boolean): Boolean;
var
Elem: TJvSimpleXMLElem;
begin
try
Elem := GetItemNamedDefault(Name, BoolToStr(Default));
if (Elem = nil) or (Elem.Value = '') then
Result := Default
else
Result := Elem.BoolValue;
except
Result := Default;
end;
end;
procedure TJvSimpleXMLElems.Clear;
var
I: Integer;
begin
if FElems <> nil then
begin
for I := 0 to FElems.Count - 1 do
begin
// TJvSimpleXMLElem(FElems.Objects[I]).Clear; // (p3) not needed -called in Destroy
FElems.Objects[I].Free;
FElems.Objects[I] := nil;
end;
FElems.Clear;
end;
end;
constructor TJvSimpleXMLElems.Create(const AOwner: TJvSimpleXMLElem);
begin
inherited Create;
FParent := AOwner;
end;
procedure TJvSimpleXMLElems.Delete(const Index: Integer);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -