Anyone have sample code to download xml using OMNIXML

I am looking for some actual working example code for reading XML using a simple OmniXml module (or OmniXmlUtils). I do not want to use the TOmniXMLWriter class, as described elsewhere, but I want to load the XML file using simple Delphi code.

I searched the OmniXml site, but the samples there are no worse than nonexistent ones.

Thanks in advance.

+2
source share
2 answers

Some common signals with OmniXML:

Loading a document from a file or from a stream or line

xml := CreateXMLDoc xml.Load(FileName); xml.LoadFromStream(XMLAsStream); xml.LoadXML(XMLAsString) 

Select one node (fifth child):

 MyNode := XML.ChildNodes.Item[4]; MyNode := XML.SelectSingleNode('Node[5]'); 

XPath selection

 MyNode := XML.SelectSingleNode('/MyNodes/SpecificNodes/Node[5]'); 

Select a collection of nodes

 MyNodes := XML.SelectNodes('/MyNodes/SpecificNodes/Node'); 

If you downloaded from http://www.omnixml.com/download.html , then there is a directory called demo inside that contains all the demos. They will explain to you almost everything.

If you decide to use SimpleStorage on top of OmniXML, let me show you what your top example will look like using SimpleStorage.

 xml := StorageFromFile(rssFileName) for channel in xml.Elements('channel') do begin ListBox1.Items.Add('['+channel.Get('title')AsStringDef+']') for Item in channel.Elements('item') do ListBox1.Items.Add(' <'+ Item .Get('title')AsStringDef+'>') end; //for iChannel 

No, you see how much template code was missing (21 lines of code were reduced to 7 for the same functionality). There is no need to check if node exists, etc., and the enums help a lot. I highly recommend you use this approach because it is much clearer.

+4
source

From the sample link, Keeper answered. These were the SelectNodes ('..') and SelectSingleNode ('...') elements that I was looking for:

 xml := CreateXMLDoc; if not xml.Load(rssFileName) then ListBox1.Items.Add('Not an XML document: '+rssFileName) else begin channels := xml.DocumentElement.SelectNodes('channel'); for iChannel := 0 to channels.Length-1 do begin channel := channels.Item[iChannel]; title := channel.SelectSingleNode('title'); if assigned(title) then ListBox1.Items.Add('['+title.Text+']') else ListBox1.Items.Add('[]'); items := channel.SelectNodes('item'); for iItem := 0 to items.Length-1 do begin title := items.Item[iItem].SelectSingleNode('title'); if assigned(title) then ListBox1.Items.Add(' <'+title.Text+'>') else ListBox1.Items.Add(' <>'); end; //for iItem end; //for iChannel 

end;

It was Sunday, and I wanted to ask for a solution before delving into the Omni source code too much.

I think the author of OmniXml should post things like sample code on his side.

Thanks.

+1
source

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


All Articles