How to search and replace tag value in XML file using delphi?

How to search and replace tag value in XML file using Delphi?

I know what an XML tag is, but the value is random and just needs to be reset to the default value, so in fact I cannot / should not look for the value, but only the tag. I also know the location of the file / files.

I'm new to Delphi, can someone provide me with a simple example of how this can be done?

Thanks in advance.

+5
source share
2 answers

I would upload the XML file using Delphi IXMLDocumentand use the document to replace the element. Something like that:

uses
  XMLDoc,
  XMLIntf;

procedure ChangeTag(const filename : String);
var
  doc : IXMLDocument;
  parent : IXMLNode;
  toReplace : IXMLNode;
  replacement : IXMLNode;
begin
  doc := LoadXMLDocument(filename);

  parent := doc.DocumentElement.ChildNodes.FindNode('parent');
  toReplace := parent.ChildNodes.FindNode('toReplace');

  replacement := doc.CreateElement('replacement', '');
  replacement.Text := toReplace.Text;

  parent.ChildNodes.ReplaceNode(toReplace, replacement);

  doc.SaveToFile(filename);
end;
+9
source

- XML, :


XML , XML () :

var
  Regex: TPerlRegEx;

Regex := TPerlRegEx.Create(nil);
Regex.RegEx := '<yourtag>.*?</yourtag>';
Result := objRegEx.Replace(inputString, replacementString, true);

TPerlRegex .


, pos, delete insert. pos > openeing), ( , ). , ; -)

+6

All Articles