XMLDocument 节点没有值

XMLDocument nodes have no values

当我用 TXMLDocument 加载 XML 时,元素不包含任何值。我用元素 <cbc:ID>TOSL108</cbc:ID> 读取了以下 example file ,它确实找到了节点,但它没有将其识别为文本元素并且值为空并且没有子元素。

  aNode := XMLDocument1.ChildNodes.Last.ChildNodes.Nodes['ID'];
  if Assigned(aNode)  then begin
    MEMOInfo.Lines.Add('Node is found');
    MEMOInfo.Lines.Add( anode.DOMNode.nodeName);
    MEMOInfo.Lines.Add( anode.DOMNode.nodeValue);
    MEMOInfo.Lines.Add( inttostr(anode.DOMNode.nodetype));
  end;

这是结果

Node is found
ID

1

原问题:

我从 xsd 创建了一个 XML 数据绑定。即 UBL-Invoice-2.1.xsd。我遵循了 this 教程。

然后我使用了 this tutorial to open the example 发票文件。并在界面中加载文件。

var
  Invoice:IXMLInvoiceType;
begin
  try   
    invoice := LoadInvoice('invoice-exameple.xml');  
  except
    on e:exception do begin
      MEMO.Lines.Add(e.Message);
    end;
  end;   
end;

现在我不知道如何访问节点,例如 <cbc:ID>TOSL108</cbc:ID> 我想尝试访问它的文本字段,但它是空的。

MEMO.Lines.Add('Factuurnummer : '+ invoice.ID.Text);

Q : 所有元素都是空的,那么如何读取这个 XML 文件?

根据DOM level 1 specification, nodeValue returns null 在一个元素节点上:

The attributes nodeName, nodeValue and attributes are included as a mechanism to get at node information without casting down to the specific derived interface. In cases where there is no obvious mapping of these attributes for a specific nodeType (e.g., nodeValue for an Element or attributes for a Comment), this returns null.

我建议导航到该元素节点的子文本节点,并在该文本节点上调用 nodeValue

XML文档没有正确处理命名空间,因此我不得不调整 XML-文件并删除命名空间。

<cbc:ID>TOSL108</cbc:ID>

变成了

<ID>TOSL108</ID>

现在可以使用了。