Michael Van Canneyt wrote:

On Sun, 3 Dec 2006, Lee wrote:

Hi,

I am trying to get up to speed with the TXMLDocument object and it seems
pretty straight forward.  However, I am having a problem retrieving the text
value of a node.

Given the following simple xml:

<?xml version="1.0" encoding="utf-8"?>
<settings>
   <login>
      <username>123</username>
      <password>123</password>
   </login>
</settings>

The following code fails to print the values (NodeValue) to the console:

var
Doc: TXMLDocument;
Node1, Node2: TDOMNode;
i: Integer
Begin
Doc := TXMLDocument.create;

if (FileExists('sys_info.xml')) then
   XMLREAD.ReadXMLFile(Doc, 'sys_info.xml');
Node1 := Doc.DocumentElement.FindNode('login');

for i := 0 to Node1.ChildNodes.Count -1 do
   begin
   writeln(node1.ChildNodes.Item[i].NodeName + ' ' +
      node1.ChildNodes.Item[i]).NodeValue);
   end;

end;

The above code correctly prints out the .NodeName, but not the .NodeValue.

Referencing the wiki/networkiing/basic example:
http://wiki.lazarus.freepascal.org/Networking#Basic_Example

It looks like .NodeValue is what I'm looking for.  Just am not sure why the
values are not being retrieved.

Because they are in a TEXT subnode of the nodes you are printing.

You should be doing:

 for i := 0 to Node1.ChildNodes.Count -1 do
   begin
   SN:=Node1.ChildNodes.Item[i];
   write(SN.NodeName + ' ');
   If SN.ChildNodes.Count>0 then
     Writeln(SN.ChildNodes[0].NodeValue)
   else
     Writeln('<empty>');
   end;



Thanks for your response, Michael. That is odd to me. I would think that a node such as <username>123</username> would be considered all one node with "123" being the NodeValue.

I've only used a couple of XML DOM/Libraries and each seems to work differently than this although its probably just semantics. For instance, with .net, I would access that value (123) with Node.InnerText. In Delphi, I use OmniXML and that library uses the syntax Node.Text. So with TXMLDocument, I assumed that given a node assignment such as <username>123</username>, that .NodeValue would privide the value within that node.

I guess I need to read up on the xml specs a bit more...

Thanks again for the response.
--

Warm Regards,

Lee

_________________________________________________________________
    To unsubscribe: mail [EMAIL PROTECTED] with
               "unsubscribe" as the Subject
  archives at http://www.lazarus.freepascal.org/mailarchives

Reply via email to