为什么Node.ChildNodes.FindNode()失败但Node.ChildNodes []成功?

时间:2012-11-14 14:55:34

标签: xml delphi dom

有人可以解释为什么ChildNodes.FindNode('')( 1 )失败但ChildNodes ['']( 2 )成功吗? ==>

代码:

const
   cNodeSOAPEnvelope  = 's:Envelope';
   cNodeSOAPBody      = 's:Body';
   cNodeSOAPBodyFault = 's:Fault';
   cNodeSOAPHeader    = 's:Header';
   cNodeFaultCode     = 'faultcode';
   cNodeFaultString   = 'faultstring';

procedure TFrmXMLParsingTests.BtnTestClick(Sender: TObject);
var
   SoapBodyNode,
   SoapBodyFaultNode,
   SoapHeaderNode,
   FaultCodeNode,
   FaultTextNode,
   RootNode: IXmlNode;
begin
   RootNode := XMLDoc.DocumentElement;
   Assert(RootNode.NodeName = cNodeSOAPEnvelope,'Root node is not SOAP envelope');
   SoapBodyNode := RootNode.ChildNodes[cNodeSOAPBody];
   SoapBodyFaultNode := SoapBodyNode.ChildNodes[cNodeSOAPBodyFault];
   if SoapBodyFaultNode <> nil then
   begin
//      FaultCodeNode := SoapBodyFaultNode.ChildNodes.FindNode(cNodeFaultCode);     (*1*)
//      FaultTextNode := SoapBodyFaultNode.ChildNodes.FindNode(cNodeFaultString);   (*1*)
      FaultCodeNode := SoapBodyFaultNode.ChildNodes[cNodeFaultCode];                (*2*) 
      FaultTextNode := SoapBodyFaultNode.ChildNodes[cNodeFaultString];              (*2*) 
      Exit; // Nothing more to do
   end; { fault in body }
   SoapHeaderNode := RootNode.ChildNodes.FindNode(cNodeSOAPHeader);
   Assert(SoapHeaderNode <> nil,'SOAP Header not found');
end;

XML:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <s:Fault>
         <faultcode xmlns:a="http://schemas.microsoft.com/exchange/services/2006/types">a:ErrorSchemaValidation</faultcode>
         <faultstring xml:lang="nl-NL">The [snip] value.</faultstring>
         <detail>
            <e:ResponseCode xmlns:e="http://schemas.microsoft.com/exchange/services/2006/errors">ErrorSchemaValidation</e:ResponseCode>
            <e:Message xmlns:e="http://schemas.microsoft.com/exchange/services/2006/errors">The request failed schema validation.</e:Message>
            <t:MessageXml xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
               <t:LineNumber>10</t:LineNumber>
               <t:LinePosition>30</t:LinePosition>
               <t:Violation>The [snip] value.</t:Violation>
            </t:MessageXml>
         </detail>
      </s:Fault>
   </s:Body>
</s:Envelope>

感谢

2 个答案:

答案 0 :(得分:3)

Read the documentation。当您访问要求不存在的节点的ChildNodes[]属性时,如果在父XMLDocument的doNodeAutoCreate属性中启用了Options标志,则会自动创建一个新节点,否则为{{ 1}}异常将被提出。

更新:由于您的文档中包含多个XML命名空间,因此在使用EXMLDocError属性和ChildNodes[]方法时需要考虑命名空间。您的代码未正确找到FindNode()<faultcode>节点的原因是因为它们具有不同的命名空间(实际上,它们根本没有任何命名空间),而<faultstring>标记具有,但<Fault>属性和1参数ChildNodes[]方法要求它们具有与其父节点相同的命名空间。在命名空间节点中搜索具有不同命名空间的子节点时,必须在搜索中包含子节点名称空间才能正确查找子节点。

试试这个:

FindNode()

答案 1 :(得分:0)

编写自己的函数:

function SearchChilds(StartNode: IXMLNode; Nodename: string): IXMLnode;
var i: Integer;
begin
   Result := nil;
   for i := 0 to StartNode.ChildNodes.Count-1 do
   begin
      if (StartNode.ChildNodes[i] <> nil) and (StartNode.ChildNodes[i].LocalName = Nodename) then
      begin
         Result := StartNode.ChildNodes[i];
         break;
      end;
      if StartNode.ChildNodes[i].HasChildNodes then
      begin
         Result := SearchChilds(StartNode.ChildNodes[i], Nodename);
         if Result<>nil then break;
      end;
   end;
end;