Solved

XML Documents

Posted on 2002-04-24
3
709 Views
Last Modified: 2012-06-27
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
Comment
Question by:jfvd
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
shaneholmes earned 200 total points
ID: 6967632
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
 

Author Comment

by:jfvd
ID: 6968329
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
 
LVL 11

Expert Comment

by:shaneholmes
ID: 6969964
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now