如何访问XML文档中的子节点值? [英] How do I access the child node values in my XML document?
问题描述
我有一个XmlString,其中的节点包含多个元素。
I have a XmlString which contains multiple elements with their nodes.
ie
<Element>
<AccountName>My Account Name</AccountName>
<FullName>Edward Jones</FullName>
</Element>
我可以访问节点名称,即AccountName,FullName,但是我无法访问值或它们返回空白。这是我的代码。
I can access the Node names ie AccountName, FullName, but I can't access the values or they return blank. Here is my code.
Doc : IXMLDocument;
begin
Doc := XMlDoc.LoadXMLData(XmlString);
Doc.DOMDocument.getElementsByTagName('Element').length; // = 11
Doc.DOMDocument.getElementsByTagName('Element').item[2].childNodes[0].nodeName; // = AccountName
Doc.DOMDocument.getElementsByTagName('Element').item[2].childNodes[0].nodeValue;
end;
我的XmlString中有11个'Element'实例,因此可以检查出来,nodeName = AccountName这是我所期望的。但是nodeValue为空。还有另一种提取值的方法吗?有人知道为什么节点值为空吗?
There are 11 instances of the 'Element' in my XmlString so this checks out, the nodeName = AccountName which is what I expect. But the nodeValue is blank. Is there another way to pull the values? Does anyone know why the node values are blank?
推荐答案
一个猜测:它看起来像标准的DOM API,因此您可以使用元素节点下方的文本节点。
A guess: It looks like standard DOM API, so you could have a Text-node below the element nodes.
Doc.DOMDocument.getElementsByTagName('Element').item[2].childNodes[0].childNodes[0].nodeValue;
这篇关于如何访问XML文档中的子节点值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!