libxml2 xmlParseFile 无法加载格式良好的 XML
作为 libxml2 的新手,我很困惑为什么 xmlParseFile 总是返回 nil
uses
libxml2;
procedure TForm1.FormCreate(Sender: TObject);
var
doc: xmlDocPtr;
begin
doc := xmlParseFile('1.xml'); // doc is nil
doc := xmlParseFile('c:\1.xml'); // doc is again nil
end;
示例 1.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<root>
</root>
据报告,XML Spy 2007 和 xmllint 工具的 xml 格式良好。
我使用 http://xmlsoft.org 中的预编译 DLL,并使用 DelphiAPI-2.6.26 和 libxml2- 尝试此代码pas-2-7-3-src 没有运气。
我想我错过了什么?
调试器只是跳过该行,因为它指向一个外部引用,如下所示:
function xmlParseFile (const filename: PChar) : xmlDocPtr; cdecl; external LIBXML2_SO;
Being new to libxml2 I am confused why xmlParseFile always returns nil
uses
libxml2;
procedure TForm1.FormCreate(Sender: TObject);
var
doc: xmlDocPtr;
begin
doc := xmlParseFile('1.xml'); // doc is nil
doc := xmlParseFile('c:\1.xml'); // doc is again nil
end;
Sample 1.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<root>
</root>
The xml is reported to be well-formed by both XML Spy 2007 and xmllint tools.
I use the precompiled DLLs from http://xmlsoft.org and tries this code with both DelphiAPI-2.6.26 and libxml2-pas-2-7-3-src without luck.
I guess I am missing something?
The debugger just jumps over the line since it points to an external reference like this:
function xmlParseFile (const filename: PChar) : xmlDocPtr; cdecl; external LIBXML2_SO;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
感谢 Remy,我将代替他给出答案来结束此问题:
将 Delphi 包装器从 PChar(XE 和 XE2 中的 PWideChar)更改为 PAnsiChar 解决了问题。一旦函数的定义被更改,
它就可以正常工作。
With all credit going to Remy I will put the answer instead of him to have this closed:
Changing the Delphi wrapper from PChar (which is PWideChar in XE and XE2) to PAnsiChar resolved the problem. Once the definition of the function was changed to
it worked fine.
我在 github https://github.com/ak47morozov/Delphi-Libxml2 上创建了项目。
修复了 Delphi XE2 上的一些编码错误。 (PChar -> PAnsiChar)
I created project on github https://github.com/ak47morozov/Delphi-Libxml2.
Fixed a few encoding bugs on Delphi XE2. (PChar -> PAnsiChar)