How can I find if a subnode exists

534 views Asked by At

This is my xml file:

<?xml version="1.0" encoding="utf-8"?>
<UsersF Ver="1.1">
    <row User="1" Pin="2y44ic" ExtPag="full"/>
    <row User="2" pin="tfde88" ExtPag="e45" />
    <row User="3" Pin="9gr444466gg" Level="nov" GamePag="3" />
</UsersF>

And this is the code that I get access and put a string-grid... I use the nextgrid ...

procedure showXmlToString;
Count:= 0;
Conf.nxtgrd.AddRow(71);
Conf.nxtgrd.BeginUpdate;
with FXml.Root do
for i := 0 to NodeCount - 1 do
  begin
    if Nodes[i].Name <> 'Ver' then
      begin
        Conf.nxtgrd.Cell[0,count].AsString := Nodes[i].Nodes[1].Value;
        Conf.nxtgrd.Cell[1,count].AsString := Nodes[i].Nodes[2].Value;
        Conf.nxtgrd.Cell[2,count].AsString := Nodes[i].Nodes[3].Value;            
        Conf.nxtgrd.Cell[3,count].AsString := Nodes[i].Nodes[4].Value;
       count := count + 1;
      end;
  end;

When it come to line that the node not exits I get the error.

1

There are 1 answers

3
sybond On

To accessing the attributes you can use TXmlNode.AttributeCount to iterate numbers of attributes available. If the main point of this code is to obtain the attributes, you can access them using TXmlNode.Containers.

Let say you want to get all the attributes name & value, you can using following iteration:

with FXML.Root do
for i := 0 to ContainerCount - 1 do
begin
  Log(Format('ContainersName=%s AtribNumber=%d',[Containers[i].Name,i]));
  for j:=0 to Containers[i].AttributeCount-1 do begin
    Log(Format('AttribName=%s AttribVal=%s',[Containers[i].Attributes[j].Name,Containers[i].Attributes[j].Value]));
  end;
end;

In this example Log procedure will display the string to the screen. As the result the output will be like this:

ContainersName=row AtribNumber=0
AttribName=User AttribVal=1
AttribName=Pin AttribVal=2y44ic
AttribName=ExtPag AttribVal=full
ContainersName=row AtribNumber=1
AttribName=User AttribVal=2
AttribName=pin AttribVal=tfde88
AttribName=ExtPag AttribVal=e45
ContainersName=row AtribNumber=2
AttribName=User AttribVal=3
AttribName=Pin AttribVal=9gr444466gg
AttribName=Level AttribVal=nov
AttribName=GamePag AttribVal=3

I hope this help you.