libxml2 xmlParseFile 无法加载格式良好的 XML

发布于 2024-12-23 05:31:24 字数 753 浏览 5 评论 0原文

作为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我是男神闪亮亮 2024-12-30 05:31:24

感谢 Remy,我将代替他给出答案来结束此问题:

将 Delphi 包装器从 PChar(XE 和 XE2 中的 PWideChar)更改为 PAnsiChar 解决了问题。一旦函数的定义被更改,

function xmlParseFile (const filename: PWideChar) : xmlDocPtr; cdecl; 
external LIBXML2_SO; 

它就可以正常工作。

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

function xmlParseFile (const filename: PWideChar) : xmlDocPtr; cdecl; 
external LIBXML2_SO; 

it worked fine.

我不吻晚风 2024-12-30 05:31:24

我在 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文