Recent

Author Topic: [SOLVED] XML multiple attributes in nodes  (Read 4487 times)

laz_inv

  • New Member
  • *
  • Posts: 10
[SOLVED] XML multiple attributes in nodes
« on: September 30, 2016, 09:59:01 am »
Hi,

I have a very rare problem, I haven't found a solution yet...

The XML file I use has the following structure:

<software Title="Software">
   <page Title="Licenses" H1="Resource Type" H2="Key" H3="User Name" H4="Company Name" H5="Key 1"  __type__="1" >
         <item Resource_Type="Microsoft Visual Studio 2010" Key="XXXXX-XXXXX-XXXXX-XXXXX-XXXXX" User_Name="" Company_Name=""  />
         <item Resource_Type="Microsoft Visual Studio 2015" Key="YYYYY-YYYYY-YYYYY-YYYYY-YYYYY" User_Name="" Company_Name=""  />
    </page>
</software>

Now I want to get access to the Resource_Type and the Key of the Resource_Type. I have no problem to navigate through the nodes but I can't read the inner values.

Thanks for any help.
« Last Edit: September 30, 2016, 10:41:21 am by laz_inv »

wp

  • Hero Member
  • *****
  • Posts: 11912
Re: XML multiple attributes in nodes
« Reply #1 on: September 30, 2016, 10:10:59 am »
If your xml library is Laz2_DOM (or Laz_DOM) coming with Lazarus then you can iterate through the Attributes of the node. This code works fine in fpspreadsheet:
Code: Pascal  [Select][+][-]
  1. function GetAttrValue(ANode : TDOMNode; AAttrName : string) : string;
  2. var
  3.   i: LongWord;
  4.   Found: Boolean;
  5. begin
  6.   Result := '';
  7.   if (ANode = nil) or (ANode.Attributes = nil) then
  8.     exit;
  9.  
  10.   Found := false;
  11.   i := 0;
  12.   while not Found and (i < ANode.Attributes.Length) do begin
  13.     if ANode.Attributes.Item[i].NodeName = AAttrName then begin
  14.       Found := true;
  15.       Result := ANode.Attributes.Item[i].NodeValue;
  16.     end;
  17.     inc(i);
  18.   end;
  19. end;
  20.  
  21. var
  22.   itemNode: TDOMNode;
  23. begin
  24.   ...
  25.   itemNode := ... // find the "item" node.
  26.   WriteLn(GetAttrValue(itemNode, 'Resource_Type'));
  27.   WriteLn(GetAttrValue(itemNode, 'Key'));
  28.   WriteLn(GetAttrValue(itemNode, 'User_Name'));
  29.  
« Last Edit: September 30, 2016, 10:14:03 am by wp »

laz_inv

  • New Member
  • *
  • Posts: 10
Re: XML multiple attributes in nodes
« Reply #2 on: September 30, 2016, 10:24:49 am »
You really made my day!

Thanks a lot, it works perfectly.

Can you please tell me how I can close this topic/mark it as solved? I am new here...  :)


wp

  • Hero Member
  • *****
  • Posts: 11912
Re: XML multiple attributes in nodes
« Reply #3 on: September 30, 2016, 10:29:30 am »
There is no "close" feature or something like that. If you want, you can edit the title of the first post and put a '[SOLVED]' in front of it.

 

TinyPortal © 2005-2018