Sese AK
Sese AK

Reputation: 105

Add Child Node with OmniXML

I'm currently using the OmniXML unit to manipulate my XML files in Delphi XE. I managed to load the XML contents from a file on the disk, now I'd like to add a child node under a specific node element. The new child node will also have an 'id' attribute. May anyone show me how to do so given that the documentation for OmniXML is very poor unfortunately! I'd also like to save the new XML in another file, may you also clarify how to do so?

Thanks, Sue

Upvotes: 2

Views: 880

Answers (2)

gabr
gabr

Reputation: 26850

uses OmniXMLUtils;

childNode := EnsureNode(parentNode, 'id');

Instead of working with raw OmniXML (and OmniXMLUtils, which is an improvement but still pretty basic) you should look at SimpleStorage and GpFluentXML.

Upvotes: 4

Ken White
Ken White

Reputation: 125748

Try something like this:

var
  iDoc, iNode: IXMLNode;
begin
  XMLDocument1.LoadFrom...(...);
  iDoc := XMLDocument1.DocumentElement;

  // Find node where you want child added     
  // and add a new child
  iNode := iDoc.ChildNodes[0].AddChild('NewNode');
  iNode.Attributes['id'] := '2';

  XMLDocument1.SaveTo...(...);
end;

BTW, it's not nice to use free, open source software and then complain about it. :) If you don't like the documentation, you can feel free to either buy a commercial product instead or contribute to the author of the open source software, or contribute some work to the documentation to improve it.

Upvotes: 2

Related Questions