The following code outputs "EOleException: error during the validation". It is this error:
0xC00CE225
XMLOM_VALIDATE_INVALID
Validate failed.
If I add the error handler the exact error message is "XML is neither valid nor invalid as no schema was found".
Does MSXML 6 SAX parser support embedding a DTD?
program TestSAXValidation;
{$APPTYPE CONSOLE}
{$R *.res}
uses
ActiveX, System.SysUtils, Winapi.msxml;
var
R: IVBSAXXMLReader;
begin
CoInitialize(nil);
try
R := CoSAXXMLReader60.Create;
R.putFeature('use-inline-schema', True);
R.putFeature('schema-validation', True);
R.putFeature('exhaustive-errors', True);
R.putFeature('prohibit-dtd', False);
R.parse('<!DOCTYPE a[<!ELEMENT a (#PCDATA)>]> <a></a>');
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
ReadLn;
end.
Related
I am attempting to copy data from a TStringStream contained in a TStreamReader into another TStringStream using the CopyFrom method. If there have been no reads of the source stream it works as advertised, however if I perform a single read of the streamreader it throws an exception with EReadError: Stream read Error. Code to show problem:
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
System.classes;
var
FStreamRead : TStreamReader;
AChar : char;
OutStream : TStringStream;
begin
FStreamRead := TStreamReader.Create(TStringStream.Create('This is test data',TEncoding.UTF8));
FStreamRead.OwnStream;
try
try
// read once
Achar := char (FStreamRead.Read);
OutStream := TStringStream.Create;
try
OutStream.CopyFrom(FStreamRead.BaseStream,4);
finally
OutStream.Free;
end;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
finally
FStreamRead.Free;
readln;
end;
end.
Commenting out the line:
Achar := char (FStreamRead.Read);
allows the copy to be done without error. The documentation states that if count is greater than zero in the TStream.CopyFrom method it performs the copy from the current position in the input stream which is what I need to achieve.
TStreamReader internally uses buffering. You are simply not allowed to use the BaseStream from outside.
I am trying to use the (Windows) clipboard in a Delphi console program, but when I try to compile I get the message
"[dcc32 Fatal Error] Clipboard_Project.dpr(6): F2613 Unit 'Clpbrd' not found."
The code looks like this:
program Clipboard_Project;
{$R *.res}
uses
System.SysUtils, Clpbrd;
var
s: String;
begin
try
s := Clipboard.AsText;
writeln(s);
readln;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
I can imagine that it's something simple and obvious, but I can't find it! Any help would be appreciated!
for correction of this question, it should be used in the uses clause Vcl.ClipBrd correctly and not Clpbrd as incorrectly typed.
The following GetProcAddress code fails when compiled under Delphi XE6 x64. It runs fine when compiled under Delphi x86. Could you help to comment what is done wrong ?
program Project11;
{$APPTYPE CONSOLE}
{$R *.res}
uses
SysUtils;
var
Library_OpenGL: LongWord;
function LoadLibrary(lpFileName: pAnsiChar): LongWord; stdcall; external 'kernel32.dll' name 'LoadLibraryA';
function GetProcAddress(hModule: LongWord; lpProcName: pAnsiChar): Pointer; stdcall; external 'kernel32.dll' name 'GetProcAddress';
begin
try
Library_OpenGL := LoadLibrary('opengl32.dll');
Assert(GetProcAddress(Library_OpenGL, 'glEnable') <> nil, 'GetProcAddress(Library_OpenGL, ''glEnable'') = nil');
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
ReadLn;
end.
Your translations are wrong. A module handle is pointer sized which explains why your erroneous translations worked on 32 bit but not 64 bit.
To correct, add the Windows unit to your uses clause, remove your declarations of LoadLibrary() and GetProcAddress(), and declare Library_OpenGL as HMODULE (which is 8 bytes in x64):
program Project11;
{$APPTYPE CONSOLE}
{$R *.res}
uses
SysUtils, Windows;
var
Library_OpenGL: HMODULE;
begin
try
Library_OpenGL := LoadLibrary('opengl32.dll');
Assert(GetProcAddress(Library_OpenGL, 'glEnable') <> nil, 'GetProcAddress(Library_OpenGL, ''glEnable'') = nil');
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
ReadLn;
end.
As an added benefit you now call the native Unicode LoadLibraryW directly rather than going via the LoadLibraryA adapter with its conversation from ANSI to the system native UTF-16.
How I can convert "02 August 2012 18:53" to DateTime?
when I use StrToDate for convert it occur error 'Invalid Date format'
You can use VarToDateTime (found in the Variants unit), which supports various time formats Delphi's RTL doesn't. (It's based on COM's date support routines like the ones used in various Microsoft products.) I tested with your supplied date, and it indeed converts it to a TDateTime properly. Tested on both Delphi 2007 and XE2.
program Project2;
{$APPTYPE CONSOLE}
{$R *.res}
uses
SysUtils, Variants;
var
DT: TDateTime;
TestDate: String;
begin
TestDate := '02 August 2012 18:53';
try
DT := VarToDateTime(TestDate);
{ TODO -oUser -cConsole Main : Insert code here }
except
on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;
Writeln(FormatDateTime('mm/dd/yyyy hh:nn', DT));
Readln;
end.
More info in the current documentation , including another sample of use (link at the bottom of that page).
Note the function in Variants unit use the default user locale. If it is not 'US' the conversion from the above string might fail. In that case you would better call VarDateFromStr directly from activex unit specifying the US locale:
uses
sysutils, activex, comobj;
var
TestDate: String;
DT: TDateTime;
begin
try
TestDate := '02 August 2012 18:53';
OleCheck(VarDateFromStr(WideString(TestDate), $0409, 0, Double(DT)));
Writeln(FormatDateTime('mm/dd/yyyy hh:nn', DT));
Readln;
except
on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;
end.
If I would like to save a IXMLDOMDocument3 in runtime to a file on my harddrive, what is the syntax for that?
E.g. like IXMLDOMDocument3.save('c:\test.xml')
Or is it even possible?
Best regards!
the sample code below demonstrates how to load and save IXMLDomDocument3 XML at runtime. It uses msxml header file from Delphi-2010. IXMLDomDocument3 inherits from IXMLDomDocument and has Save method (as you wrote in your question). If method parameter is a string, then it specifies file name (it creates or replaces target file).
program Project3;
{$APPTYPE CONSOLE}
uses SysUtils, msxml, comObj, activex;
procedure LoadAndSaveXML(LoadFile, SaveFile : string);
var xml : IXMLDOMDocument3;
tn : IXMLDOMElement;
begin
xml := CreateComObject(CLASS_DOMDocument60) as IXMLDOMDocument3;
xml.load(LoadFile);
xml.save(SaveFile);
end;
begin
try
CoInitialize(nil);
try
LoadAndSaveXML('D:\in.xml', 'D:\out.xml');
finally
CoUninitialize();
end;
except
on E: Exception do begin
Writeln(E.ClassName, ': ', E.Message);
readln;
end;
end;
end.