简体   繁体   English

在Delphi XE2中解析XML时发生访问冲突

[英]Access Violation while parsing XML in Delphi XE2

I'm trying to parse a MusicBraninz XML file in Delphi XE2 using the following code: 我正在尝试使用以下代码在Delphi XE2中解析MusicBraninz XML文件:

webquery := 'http://www.musicbrainz.org/ws/2/recording/?query='+escape(tracktitle)+'&artist:'+escape(ArtistTitle);

Log('WebQuery: ' + webquery, 0);

begin
  XMLDoc:= TXMLDocument.Create(nil);
  XMLDoc.FileName := webQuery;
  XMLDoc.Active := True;

  Log('Report: ' + XMLDoc.XML.Text, 0);


   StartItemNode := XMLDoc.DocumentElement.ChildNodes.First.ChildNodes.FindNode('release-list') ;

   ANode := StartItemNode;
   repeat
     Result.Album := ANode.ChildNodes['title'].Text;  <-- Access Violation
     Result.Status:= ANode.ChildNodes['status'].Text;

     ANode := ANode.NextSibling;
   until ANode = nil;

end;

The XML file is fetched correctly and looks like what's below: 正确提取了XML文件,如下所示:

<metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#" xmlns:ext="http://musicbrainz.org/ns/ext#-2.0">
<recording-list offset="0" count="1">
<recording ext:score="100" id="a399eec1-d45d-4505-b475-ead0da6cad17">
<title>Mestecăniș</title>
<length>359000</length>
<artist-credit>
<name-credit>
<artist id="8fb78a16-0cba-4175-8c92-d9645dfb007d">
<name>Bucovina</name>
<sort-name>Bucovina</sort-name>
</artist>
</name-credit>
</artist-credit>
<release-list>
<release id="22b00afc-86ea-445a-8805-b6bfa33da74e">
<title>Duh</title>
<status>Official</status>
<release-group type="EP" id="4e8fb87c-3760-48c1-a3d7-88e7a2c839fa">
<primary-type>EP</primary-type>
</release-group>
<date>2010</date>
<country>RO</country>
<medium-list>
<track-count>5</track-count>
<medium>
<position>1</position>
<format>CD</format>
<track-list offset="3" count="5">
<track>
<number>4</number>
<title>Mestecăniș</title>
<length>359000</length>
</track>
</track-list>
</medium>
</medium-list>
</release>
</release-list>
</recording>
</recording-list>
</metadata>

My question is: am I doing anything wrong here? 我的问题是:我在这里做错什么了吗? All variables are declared and initialized OK. 声明并初始化所有变量。

Thanks, 谢谢,

You have an access violation because the FindNode method is returning a nil value and you are trying to access of a invalid memory location. 您遇到访问冲突,因为FindNode方法返回一个nil值,并且您试图访问无效的内存位置。 In order to use the FindNode method you must check the hierarchy (level) of the nodes to search and then check if the result is not nil . 为了使用FindNode方法,必须检查要搜索的节点的层次结构(级别),然后检查结果是否不是nil

Try this sample. 试试这个样本。

  XMLDoc:= LoadXMLDocument(webQuery);
   StartItemNode := XMLDoc.DocumentElement.ChildNodes.FindNode('recording-list');
   if not Assigned(StartItemNode) then exit;
   StartItemNode := StartItemNode.ChildNodes.FindNode('recording');
   if Assigned(StartItemNode) then
   begin
     StartItemNode := StartItemNode.ChildNodes.FindNode('release-list');
     if Assigned(StartItemNode) then
     begin
       StartItemNode := StartItemNode.ChildNodes.FindNode('release');
       if Assigned(StartItemNode) then
       begin
         ANode := StartItemNode;
         repeat
           Result.Album := ANode.ChildNodes['title'].Text;
           Result.Status:= ANode.ChildNodes['status'].Text;
           ANode := ANode.NextSibling;
         until ANode = nil;
       end;
     end;
   end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM