gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 02:31:17 25 4
gpt4 key购买 nike

谁能解释为什么 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>

谢谢

最佳答案

Read the documentation .当您访问 ChildNodes[]属性要求一个不存在的节点,如果 doNodeAutoCreate 将自动创建一个新节点标志在父 XMLDocument 的 Options 中启用属性,否则为 EXMLDocError相反,将引发异常。

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

试试这个:

uses
..., XmlIntf, XMLDom, XmlDoc;

const
cSoapEnvelopeNS = 'http://schemas.xmlsoap.org/soap/envelope/';
cExchangeErrorsNS = 'http://schemas.microsoft.com/exchange/services/2006/errors';
cExchangeTypesNS = 'http://schemas.microsoft.com/exchange/services/2006/types';

cNodeSOAPEnvelope = 'Envelope';
cNodeSOAPBody = 'Body';
cNodeSOAPBodyFault = 'Fault';
cNodeSOAPHeader = 'Header';
cNodeFaultCode = 'faultcode';
cNodeFaultString = 'faultstring';
cNodeDetail = 'detail';
cNodeResponseCode = 'ResponseCode';
cNodeMessage = 'Message';
cNodeMessageXml = 'MessageXml';
cNodeLineNumber = 'LineNumber';
cNodeLinePosition = 'LinePosition';
cNodeViolation = 'Violation';

procedure TFrmXMLParsingTests.BtnTestClick(Sender: TObject);
var
RootNode,
SoapBodyNode,
SoapBodyFaultNode,
SoapHeaderNode,
FaultCodeNode,
FaultTextNode,
DetailNode,
ResponseCodeNode,
MessageNode,
MessageXmlNode,
LineNumberNode,
LinePositionNode,
ViolationNode: IXMLNode;
begin
RootNode := XMLDoc.DocumentElement;
Assert(NodeMatches(RootNode.DOMNode, cNodeSOAPEnvelope, cSoapEnvelopeNS), 'Root node is not SOAP envelope');

// these have the same namespace as <Envelope>
SoapBodyNode := RootNode.ChildNodes[cNodeSOAPBody];
SoapBodyFaultNode := SoapBodyNode.ChildNodes.FindNode(cNodeSOAPBodyFault);

if SoapBodyFaultNode <> nil then
begin
// these have a different namespace than <Fault>!
FaultCodeNode := SoapBodyFaultNode.ChildNodes.FindNode(cNodeFaultCode, '');
FaultTextNode := SoapBodyFaultNode.ChildNodes.FindNode(cNodeFaultString, '');
DetailNode := SoapBodyFaultNode.ChildNodes.FindNode(cNodeDetail, '');

if DetailNode <> nil then
begin
// these have different namespaces than <detail>!
ResponseCodeNode := DetailNode.ChildNodes.FindNode(cNodeResponseCode, cExchangeErrorsNS);
MessageNode := DetailNode.ChildNodes.FindNode(cNodeMessage, cExchangeErrorsNS);
MessageXmlNode := DetailNode.ChildNodes.FindNode(cNodeMessageXml, cExchangeTypesNS);

if MessageXmlNode <> nil then
begin
// these have the same namespace as <MessageXml>
LineNumberNode := MessageXmlNode.ChildNodes.FindNode(cNodeLineNumber);
LinePositionNode := MessageXmlNode.ChildNodes.FindNode(cNodeLinePosition);
ViolationNode := MessageXmlNode.ChildNodes.FindNode(cNodeViolation);
end;
end;
Exit;
end;

// this has the same namespace as <Envelope>
SoapHeaderNode := RootNode.ChildNodes.FindNode(cNodeSOAPHeader);
Assert(SoapHeaderNode <> nil,'SOAP Header not found');
end;

关于xml - 为什么 Node.ChildNodes.FindNode() 失败但 Node.ChildNodes[] 成功?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13381241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com