Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 720
  • Last Modified:

XML Documents

Hi there

I can parse an existing XML document using the TXMLDocument component that ships with Delphi but I've got trouble creating a new document using the component.

I've tried createnode / and addnode but then most of the properties and methods are grayed out.

Please help
 If possible a small bit of sample code would help a lot.
  Fick
0
jfvd
Asked:
jfvd
  • 2
1 Solution
 
shaneholmesCommented:
Here is a simple sample

TForm1 = class(TForm)
    btnSimple: TButton;
    Memo1: TMemo;
    procedure btnSimpleClick(Sender: TObject);
   rivate
    { Private declarations }
  public
    { Public declarations }
  end;


procedure TForm1.btnSimpleClick(Sender: TObject);
var
  iXml: IDOMDocument;
  iRoot, iNode, iNode2, iChild, iAttribute: IDOMNode;
begin
  // empty the document
  XMLDoc.Active := False;
  XMLDoc.XML.Text := '';
  XMLDoc.Active := True;

  // root
  iXml := XmlDoc.DOMDocument;
  iRoot := iXml.appendChild (iXml.createElement ('xml'));
  // node "test"
  iNode := iRoot.appendChild (iXml.createElement ('test'));
  iNode.appendChild (iXml.createElement ('test2'));
  iChild := iNode.appendChild (iXml.createElement ('test3'));
  iChild.appendChild (iXml.createTextNode('simple value'));
  iNode.insertBefore (iXml.createElement ('test4'), iChild);

  // node replication
  iNode2 := iNode.cloneNode (True);
  iRoot.appendChild (iNode2);

  // add an attribute
  iAttribute := iXml.createAttribute ('color');
  iAttribute.nodeValue := 'red';
  iNode2.attributes.setNamedItem (iAttribute);

  // show XML in memo
  Memo1.Lines.Text := FormatXMLData (XMLDoc.XML.Text);
end;


Shane
0
 
jfvdAuthor Commented:
Thanx Shane

I actually got it to work now.

Do you always have to use the DOMDocument to create "XML"? Also do you perhaps know what will happen if I've got a really large document to build before saving it to disk. Wouldn't I run into resource problems

Fick
0
 
shaneholmesCommented:
I always use it. You can of course create on by hand, but... Like always, applications are dependent on memory, therefore, you would have to test how large of a document you can store with the resources you have, or the potential end user will have.

Shane
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now