我是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工具格式良好。
我使用来自http://xmlsoft.org的预编译DLL,并尝试同时使用DelphiAPI-2.6.26和libxml2-pas-2-7-3-src的此代码,但不走运。
我想我缺少了什么?
调试器仅跳过该行,因为它指向这样的外部引用:
function xmlParseFile (const filename: PChar) : xmlDocPtr; cdecl; external LIBXML2_SO;
最佳答案
归功于雷米,我将用答案代替他来结束这个问题:
将Delphi包装器从PChar(在XE和XE2中为PWideChar)更改为PAnsiChar可以解决此问题。一旦函数的定义更改为
function xmlParseFile (const filename: PWideChar) : xmlDocPtr; cdecl;
external LIBXML2_SO;
工作正常。
关于delphi - libxml2 xmlParseFile无法加载格式正确的XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8667355/