Reputation: 16116
I have a XmlString which contains multiple elements with their nodes.
ie
<Element>
<AccountName>My Account Name</AccountName>
<FullName>Edward Jones</FullName>
</Element>
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;
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?
Upvotes: 0
Views: 8585
Reputation: 595377
You are dropping down all the way to the low-level DOM level. In that regard, @MizardX's response is correct - the text is contained in its own distinct child node that you have to access directly. However, since you are using IXMLDocument
, you don't need to drop that far down. The IXMLNode
interface is higher up and hides those details from you, providing easier access to nodes and their data, eg:
var
Doc : IXMLDocument;
ElementNode, AccountNameNode, FullNameNode : IXMLNode;
Count: Integer;
NodeName, NodeText: String;
begin
Doc := LoadXMLData(XmlString);
ElementNode := Doc.DocumentElement;
Count := ElementNode.ChildNodes.Count; // = 2
AccountNameNode := ElementNode.ChildNodes[0];
NodeName := AccountNameNode.NodeName;
NodeText := AccountNameNode.Text;
FullNameNode := ElementNode.ChildNodes[1];
NodeName := FullNameNode.NodeName;
NodeText := FullNameNode.Text;
end;
Upvotes: 3
Reputation: 89171
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;
Upvotes: 2