How to create an XML file using TXML Document in Delphi 7 - delphi

I have used the following code to create the XML Document :
procedure TForm1.btnCreateXMLClick(Sender: TObject);
var
rootName:string;
childName:string;
attrChild:string;
iXml: IDOMDocument;
iRoot, iNode, iNode2, iChild, iAttribute: IDOMNode;
begin
XMLDoc.Active:=false;
XMLDoc.XML.Text:='';
XMLDoc.Active:=true;
XMLDoc.FileName:='C:\Documents and Settings\a\Desktop\New Text Document.xml';
iXml := XmlDoc.DOMDocument;
//iRoot:=iXml.documentElement(iXml.createElement('xml'));
iRoot := iXml.appendChild(iXml.createElement ('xml'));
// node "test"
iNode := iRoot.appendChild (iXml.createElement ('test'));
iNode.appendChild (iXml.createElement ('test2'));
iChild := iNode.appendChild (iXml.createElement ('test3'));
iChild.appendChild (iXml.createTextNode('simple value'));
iNode.insertBefore (iXml.createElement ('test4'), iChild);
// node replication
iNode2 := iNode.cloneNode (True);
iRoot.appendChild (iNode2);
// add an attribute
iAttribute := iXml.createAttribute ('color');
iAttribute.nodeValue := 'red';
iNode2.attributes.setNamedItem (iAttribute);
// show XML in memo
memXMLOutput.Lines.Text:=FormatXMLData(XMLDoc.XML.Text);
end;
I get the output in memXMLOutput but the XML document does not show the output when seen in Notepad ot IE. where is the problem? Thanks in advance

Remove this:
XMLDoc.FileName:='C:\Documents and Settings\a\Desktop\New Text Document.xml';
and add something like this after the code is done creating the XML document:
XMLDoc.SaveToFile('C:\Documents and Settings\a\Desktop\New Text Document.xml');

Related

How to get the subitem value from a TRESTReponse in Delphi?

I need to get the subitem1 value of a LResponse that contains following JSON:
"Information": {
"subitem1": "2011",
"subitem2": "Test"
}
I am using this code to get the other values, it's working good, but when I try to get the Information, subitem1 or subitem2, it's returning a empty value.
var
LClient: TRESTClient;
LRequest: TRESTRequest;
LResponse: TRESTResponse;
begin
LClient := TRESTClient.Create(URL_API);
try
LRequest := TRESTRequest.Create(LClient);
try
LResponse := TRESTResponse.Create(LClient);
try
LRequest.Client := LClient;
LRequest.Response := LResponse;
LRequest.Method := rmGET;
LRequest.Params.AddHeader('Authorization','Bearer '+FToken);
LRequest.Params.ParameterByName('Authorization').Options := [poDoNotEncode];
LRequest.Params.AddHeader('Accept', 'application/json');
LRequest.Execute;
LResponse.GetSimpleValue('subitem1', FLogradouro);
{ ... }
GetSimpleValue is only able to read top level properties of JSON in response. It is also extremely inefficient, because it parses JSON upon every call to the method.
TRESTResponse already gives you access to parsed JSON via its JSONValue property. It allows you to query values within the whole structure using JSON path. Accessing JSONValue property will parse the response only once, but beware that it can return nil, if the response is empty or not a valid JSON.
Another point is that you don't have to create TRestResponse yourself. It is automatically created in LRequest.Execute. With that said the code to access values in the returned JSON would be:
if not Assigned(LRequest.Response.JSONValue) then
raise Exception.Create('Invalid response.');
ShowMessage(LRequest.Response.JSONValue.GetValue<string>('Information.subitem1'));
You can use square brackets in JSON path as array accessor to access items by index:
LRequest.Response.JSONValue.GetValue<string>('SomeStrings[0]');
You can do:
RESTResponse1.JSONValue.GetValue<string>('Information.subitem1');
or
var
LValue: string;
LJSONObject: TJSONObject;
begin
LJSONObject := RESTResponse1.JSONValue as TJSONObject;
LValue := LJSONObject.GetValue('subitem1').Value;
end;

How to set the TRichedit default paragraph background color

I try to set the TRichEdit control default paragraph bg color with this code:
//******************************************************************************
class procedure TRichEditUtility.setBGColor( aRE_ : TTNTRichEdit; bgColor_ : cardinal; default_ : boolean = FALSE );
//******************************************************************************
var
cf: TCharFormat2;
begin
if ( aRE_ <> NIL ) then
begin
fillchar(cf, sizeof(cf), 0);
cf.cbSize := sizeof( cf );
cf.dwMask := CFM_BACKCOLOR;
cf.crBackColor := bgColor_;
if ( default_ ) then
aRE_.Perform( EM_SETCHARFORMAT, SPF_SETDEFAULT, lparam(#cf) )
else
aRE_.Perform( EM_SETCHARFORMAT, SCF_SELECTION, lparam(#cf) );
end else
raise EInvalidInputParameter.create_string( 'TRichEditUtility', 'setBGColor', 'aRE_', CONST_chars_NIL );
end;
But the value of the SPF_SETDEFAULT constant is unknown!
Can somebody tell me its value? (Or the file name which define its value)
Here is how to resolve this issue, and any issue of this nature.
Perform a websearch for SPF_SETDEFAULT.
This takes you to the documentation for EM_SETCHARFORMAT.
That document lists Richedit.h as the required header.
Locate Richedit.h in your copy of the Windows SDK and search for SPF_SETDEFAULT.
That search yields this: #define SPF_SETDEFAULT 0x0004.
So in Delphi you define the constant like this: const SPF_SETDEFAULT = $0004.

Meaning of error TSimpleCodec.Begin_EncryptMemory - Wrong mode

I am using Delphi XE6 and LockBox 3.4.3 to run the code actEncryptStringExecute. This code was posted as an answer to the question 'How to use AES-256 encryption in lockbox 3 using delphi'.
The error I get is TSimpleCodec.Begin_EncryptMemory - Wrong mode.
There is another question 'TSimpleCodec.Begin_EncryptMemory - Wrong mode' where the answer is "You don't need to do this if you are setting up the codec with design-time values. It's much easier to do at design-time. Just set the published properties as required".
TCodec properties are :-
AdvancedOptions2 = []
AsymetricKeySizeInBits = 1024
ChainMode = ECB (with block padding)
Cipher = Base64
CryptoLibrary = CryptographicLibrary1
Encoding = (TEncoding)
TCryptographicLibrary properties are :-
CustomCipher = (TCustomStreamCipher)
Name = CryptographicLibrary1
ParentLibrary =
The code is :-
var
base64CipherText : String;
PlainTextStr : String;
ReconstructedPlainTextStr : String;
procedure TForm1.btnEncryptClick(Sender: TObject);
begin
PlainTextStr := edtPlainText.Text;
Codec1.EncryptString(PlainTextStr, base64CipherText, TEncoding.Unicode);
lblEncrypted.Caption := base64CipherText;
Codec1.DecryptString(ReconstructedPlainTextStr, base64CipherText, TEncoding.Unicode);
lblReconstructed.Caption := base64CipherText;
end;
What do I need change at design time to get this most simple example to work?

How to transfer a scanned image from WIA ImageFile to TBitmap without saving it to disk?

I'm using this code to acquire the scanned image from WIA:
const
wiaFormatJPEG = '{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}';
wiaFormatPNG = '{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}';
var
CommonDialog: ICommonDialog;
AImage: IImageFile;
i: Integer;
begin
CommonDialog := CreateOleObject('WIA.CommonDialog') as ICommonDialog;
for i := 1 to Scanner.Properties.Count do
begin
if (Scanner.Properties[i].Name = 'Horizontal Resolution') or
(Scanner.Properties[i].Name = 'Vertical Resolution') then
Scanner.Properties[i].Set_Value(72)
else if Scanner.Properties[i].Name = 'Horizontal Extent' then
Scanner.Properties[i].Set_Value(Round(8.27 * 72))
else if Scanner.Properties[i].Name = 'Vertical Extent' then
Scanner.Properties[i].Set_Value(Round(11.00 * 72));
end;
AImage := IUnknown(CommonDialog.ShowTransfer(Scanner, wiaFormatPNG, True)) as IImageFile;
//Save the image
AImage.SaveFile('D:\1.' + AImage.FileExtension);
imgImage.Picture.LoadFromFile('D:\1.' + AImage.FileExtension);
DeleteFile('D:\1.' + AImage.FileExtension);
end;
Scanner is initialized using this code:
Scanner := DevMgr.DeviceInfos[Integer(cbWIASource.Items.Objects[cbWIASource.ItemIndex])].Connect.Items[1];
And DevMgr and cbWIASource are initialized using this code:
DevMgr := CreateOleObject('WIA.DeviceManager') as IDeviceManager;
for i := 1 to DevMgr.DeviceInfos.Count do
for j := 1 to DevMgr.DeviceInfos[i].Properties.Count do
if DevMgr.DeviceInfos[i].Properties[j].Name = 'Name' then
begin
cbWIASource.Items.AddObject(DevMgr.DeviceInfos[i].Properties[j].Get_Value, TObject(i));
Break;
end;
I was wondering if there is a way to copy the scanned document without first saving it to the disk. I read on MSDN that I can access ARGBData member of ImageFile to access pixel data, but is there a simple way to copy the entire image from FileData to TBitmap? For instance, can I use a TMemoryStream?
Just as an update, I found this example on MSDN. I know nothing about VB, but I guess the Picture object is a wrapper around HBITMAP. So, is it logical to conclude that the ImageFile.Picture property is what I need?
IImageFile has a property FileData with provides access to the binary image data, via IVector.BinaryData

WordApplication Document add text

How to paste some text to Word document if i have handle to TWordDocument or _Document?
Paste to the Range object of the document:
Document.Range(EmptyParam, EmptyParam).Paste;
or
Word.ActiveDocument.Range(EmptyParam, EmptyParam).Paste;
You can tell where to paste:
var
R: OleVariant;
..
R := 20;
Document.Range(R, R).Paste;

Resources