![1868978 | Embarcadero RAD Studio Delphi C++Builder Blogs](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2020/08/1868978-1.jpg?resize=1140%2C694&ssl=1)
- Introduction
XML is a VCL-type library (Windows platform only). Its main purpose is to create and parse XML documents. If you need installation instructions watch the video below.
2. Components in the Demo and what they do
- ButtonReadXml – when clicked, it creates XMLReader object. A while cycle adds live by line the elements, checking the attributes count in the XML document. To convert the Nodes into readable text (string), is used ‘NodeToString’ function. The converted text into string is shown in the TMemo field. The full code on the button click can be seen in the next lines:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
Memo.Lines.Clear; with TXmlReader.Create('document.xml') do try while Read do begin Text := IntToStr(Line) + ',' + IntToStr(Position) + ': ' + NodeToString(Node); case Node of xnElement: begin Text := Text + ': ' + Name; Element; FirstAttribute; for I := 1 to AttributeCount do begin Text := Text + ' ' + Name + '=' + Value; NextAttribute; end; end; xnText: Text := Text + ': ' + Value; xnCData: Text := Text + ': ' + Value; xnComment: Text := Text + ': ' + Value; xnWhitespace: Text := Text + ': "' + Value + '"'; xnEndElement: Text := Text + ': ' + Name; xnXmlDeclaration: Text := Text + ': ' + Name; end; Memo.Lines.Add(Text); end finally Free; end; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<strong>f</strong>unction NodeToString(Node: TXmlNode): string; begin case Node of xnNone: Result := 'None'; xnElement: Result := 'Element'; xnAttribute: Result := 'Attribute'; xnText: Result := 'Text'; xnCData: Result := 'CData'; xnProcessingInstruction: Result := 'Processing instruction'; xnComment: Result := 'Comment'; xnDocumentType: Result := 'Document type'; xnWhitespace: Result := 'White space'; xnEndElement: Result := 'End element'; xnXmlDeclaration: Result := 'Declaration'; else Result := ''; end; end; |
- ButtonWriteXmlClick – when clicked, it creates TXmlWriter object. Then it start adding elements and attributes to create the XML document. You can check it in the next lines:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
with TXmlWriter.Create('document.xml') do try Indent := True; StartDocument; StartElement('element1'); Comment('comment'); StartElement('element2'); Attribute('attribute1', 'value1'); Attribute('attribute2', 'value2'); Text('Hello, world!'); EndElement; StartElement('element3'); CData('data'); EndElement; EndElement; EndDocument; finally Free; end; ShowMessage('document.xml created'); |
A short video shows the library in action.
You can download the Demo here.
Like what you see? You can get the XML Library and over 100 other fantastic WinSoft components with our Enterprise Component Pack. For a limited time, when you purchase RAD Studio Enterprise or Architect Edition at special Upgrade Pricing, you will also get this package of third-party software worth over $13,000, including the full WinSoft Component Library, at NO EXTRA COST! Step up to RAD Studio 10.4.1 today!
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition