Recent

Author Topic: XML: modify values from index number  (Read 2188 times)

rretamar

  • New Member
  • *
  • Posts: 39
XML: modify values from index number
« on: June 24, 2018, 05:42:45 am »
Hi.
I have a xml file with multiple values, with attribute unique index numbers. Can easy read or write  the values in any position using tag numner ? for example 00080005.

<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<!-- Specific Character Set -->
  <attr tag="00080005">ISO_IR 100</attr>
<!-- Scheduled Procedure Step Sequence -->
  <attr tag="00400100">
    <item>
<!-- Scheduled Station AE Title -->
      <attr tag="00400001">NODENAME</attr>
<!-- Scheduled Procedure Step Start Date -->
      <attr tag="00400002">20180623</attr>
<!-- Scheduled Procedure Step Start Time -->
      <attr tag="00400003">0910</attr>
<!-- Modality -->
      <attr tag="00080060">MR</attr>
......

Thanks.
« Last Edit: June 24, 2018, 06:08:37 am by rretamar »

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: XML: modify values from index number
« Reply #1 on: June 24, 2018, 03:02:32 pm »
hello,
you can try to use the units of the fcl-xml and xpath to find the node.
Example :
Input xml File :
Code: XML  [Select][+][-]
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <dataset>
  3.   <!-- Specific Character Set -->
  4.   <attr tag="00080005">ISO_IR 100</attr>
  5.   <!-- Scheduled Procedure Step Sequence -->
  6.   <attr tag="00400100">
  7.     <item>
  8.       <!-- Scheduled Station AE Title -->
  9.       <attr tag="00400001">NODENAME</attr>
  10.       <!-- Scheduled Procedure Step Start Date -->
  11.       <attr tag="00400002">20180623</attr>
  12.       <!-- Scheduled Procedure Step Start Time -->
  13.       <attr tag="00400003">0910</attr>
  14.       <!-- Modality -->
  15.       <attr tag="00080060">MR</attr>
  16.     </item>
  17.   </attr>
  18. </dataset>
  19.  

The  code :
Code: Pascal  [Select][+][-]
  1. // uses DOM, XMLRead, XMLWrite, XPath;  
  2. // ...
  3. procedure TForm1.Button2Click(Sender: TObject);
  4. var
  5.   Xml: TXMLDocument;
  6.   XPathResult: TXPathVariable;
  7.   TheNodeSet : TNodeSet;
  8. begin
  9.   // read input xml file
  10.    ReadXMLFile(Xml, 'xmlFile.xml');
  11.   // Find Node with tag value 00080005
  12.   XPathResult := EvaluateXPathExpression('//attr[@tag="00080005"]', Xml.DocumentElement);
  13.   TheNodeSet := XPathResult.AsNodeSet;
  14.   if TheNodeSet.Count = 1 then
  15.     begin
  16.       ShowMessage(String(XPathResult.AsText));
  17.       // Change Value of Node with tag value = 00080005
  18.       TDomNode(TheNodeSet.First).TextContent := 'ISO_IR 100_rretamar';
  19.     end;
  20.   // write in an output xml file
  21.   WriteXMLFile(Xml,'out.xml');
  22.   XPathResult.Free;
  23.   Xml.Free;
  24. end;

Result xml file :
Code: XML  [Select][+][-]
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <dataset>
  3.   <!-- Specific Character Set -->
  4.   <attr tag="00080005">ISO_IR 100_rretamar</attr>
  5.   <!-- Scheduled Procedure Step Sequence -->
  6.   <attr tag="00400100">
  7.     <item>
  8.       <!-- Scheduled Station AE Title -->
  9.       <attr tag="00400001">NODENAME</attr>
  10.       <!-- Scheduled Procedure Step Start Date -->
  11.       <attr tag="00400002">20180623</attr>
  12.       <!-- Scheduled Procedure Step Start Time -->
  13.       <attr tag="00400003">0910</attr>
  14.       <!-- Modality -->
  15.       <attr tag="00080060">MR</attr>
  16.     </item>
  17.   </attr>
  18. </dataset>
  19.  

Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

rretamar

  • New Member
  • *
  • Posts: 39
Re: XML: modify values from index number
« Reply #2 on: June 24, 2018, 10:09:44 pm »
Work fine !!!!

Thanks.  :)

 

TinyPortal © 2005-2018