How to convert code between OmniXML and Delphi's own XML library?

I recently started using OmniXML primarily because it can be used for both Delphi and Lazarus.

I myself am a newbie when it comes to XML, and it is here that I hope that I can learn something or avoid any bad things that I may already be doing.

For this, I'm going to use another question that I have as a reference: Saving and loading Treeview using XML

In one of bummi's answers , I think he uses standard XML in Delphi, where I use OmniXML in Lazarus, so the code that he posted in his answer will not compile. Now I am working on changing the code, but I need to know if the following is correct:


(1) Variable types

Delphi

TTreeToXML = Class private FDOC: TXMLDocument; FRootNode: IXMLNode; 

OmniXML

 TTreeToXML = Class private FDOC: IXMLDocument; FRootNode: IXMLElement; 

(2) Creating an XML Document

Delphi

 FDOC := TXMLDocument.Create(nil); 

OmniXML

 FDOC := CreateXMLDoc; 

(3) Release XML document

Delphi

 if Assigned(FDOC) then FDOC.Free; 

OmniXML

I don’t see a way to free the document?


(4) Attributes

Delphi

 Procedure TTreeToXML.WriteNode(N: TTreeNode; ParentXN: IXMLNode); var CurrNode: IXMLNode; Child: TTreeNode; begin CurrNode := ParentXN.AddChild(N.Text); CurrNode.Attributes['NodeLevel'] := N.Level; CurrNode.Attributes['Index'] := N.Index; Child := N.getFirstChild; while Assigned(Child) do begin WriteNode(Child, CurrNode); Child := Child.getNextSibling; end; end; 

OmniXML

 Procedure TTreeToXML.WriteNode(N: TTreeNode; ParentXN: IXMLNode); var CurrNode: IXMLNode; Child: TTreeNode; begin CurrNode := ParentXN.AddChild(N.Text); CurrNode.Attributes.SetValue('NodeLevel', IntToStr(N.Level)); CurrNode.Attributes.SetValue('NodeIndex', IntToStr(N.Index)); Child := N.getFirstChild; while Assigned(Child) do begin WriteNode(Child, CurrNode); Child := Child.getNextSibling; end; end; 

(5) Options

Delphi

 FDOC.Options := FDOC.Options + [doNodeAutoIndent]; 

OmniXML

The document is automatically indented, I can not find any parameters?


(6) Active

Delphi

 FDOC.Active := true; 

OmniXML

I see no way to set such a property to True or False?


(7) Coding

Delphi

 FDOC.Encoding := 'UTF-8'; 

OmniXML

Again I do not see such an opportunity?


Basically, I would like to know what the differences are between the Delphi XML and OmniXML implementations.

Are the changes I made right or not?

Properties that I cannot find, such as "Parameters" and "Encoding", how to implement this in OmniXML?

Thanks.

+6
source share
1 answer

(1) Variable types

 TTreeToXML = Class private FDOC: IXMLDocument; FRootNode: IXMLNode; 

(2) Creating an XML Document

OK

(3) Release XML document

No need to free. Its interface is based. You can explicitly free it like this:

 FDOC := nil; 

if you have no other links to it.

(4) Attributes

Maybe ok. I did not pay attention to it.

(5) Options

You control indentation when saving an XML document.

 procedure TXMLDocument.Save(const FileName: string; const OutputFormat: TOutputFormat = ofNone); 

This is what for OutputFormat. Also check the PreserveWhiteSpace property when loading XML from a file or stream.

(6) Active

What is Active? I do not see the need for this.

(7) Coding

Using:

 function CreateProcessingInstruction(const Target, Data: XmlString): IXMLProcessingInstruction; 

To write it, for example, as follows:

 <?xml version="1.0" encoding="UTF-8" ?> 

This is if you save the document and want to specify the encoding. For reading, OmniXML can read almost any encoding, provided that there is a specification.


In any case, OmniXML is very similar to TXMLDocument. The changes are mainly related to the programmers interface, and OmniXML is compatible with MSXML.

You can also check out SimpleStorage , which is a set of interfaces on top of OmniXML that simplify its LOT. Just check out the demo and see what I mean. But, unfortunately, he does not work under Lazarus.

+6
source

Source: https://habr.com/ru/post/953052/


All Articles