Last active
January 27, 2022 10:55
-
-
Save xavierlopezpujol/a847eee419f074a2f780c2db7c51146b to your computer and use it in GitHub Desktop.
Parse XML with MSXML with and without thread
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program Xml_amb_thread; | |
{$APPTYPE CONSOLE} | |
{$R *.res} | |
uses | |
System.SysUtils,System.win.ComObj, Classes, XMLIntf, XMLDoc,ActiveX,Xml.xmldom , Xml.Win.msxmldom; | |
const Xml= | |
'<?xml version="1.0"?>'+ | |
'<catalog>'+ | |
' <book id="bk101">'+ | |
' <author>Gambardella, Matthew</author>'+ | |
' <title>XML Developer''s Guide</title>'+ | |
' <genre>Computer</genre>'+ | |
' <price>44.95</price>'+ | |
' <publish_date>2000-10-01</publish_date>'+ | |
' <description>An in-depth look at creating applications with XML.</description>'+ | |
' </book>'+ | |
' <book id="bk102">'+ | |
' <author>Ralls, Kim</author>'+ | |
' <title>Midnight Rain</title>'+ | |
' <genre>Fantasy</genre>'+ | |
' <price>5.95</price>'+ | |
' <publish_date>2000-12-16</publish_date>'+ | |
' <description>A former architect battles corporate zombies an evil sorceress, and her own childhood to become queen of the world.</description>'+ | |
' </book>'+ | |
'</catalog>'; | |
procedure ParseXML; | |
var Document: IXMLDocument; | |
Catalog, Book : IXMLNode; | |
i: Integer; | |
begin | |
Document := TXMLDocument.Create(nil); | |
Document.LoadFromXML(xml); | |
Catalog := Document.ChildNodes.FindNode('catalog'); | |
Assert(Catalog.ChildNodes.Count=2); | |
Book := Catalog.ChildNodes[0]; | |
Assert(Book.ChildNodes['author'].Text='Gambardella, Matthew'); | |
Book := Catalog.ChildNodes[1]; | |
Assert(Book.ChildNodes['genre'].Text='Fantasy'); | |
end; | |
procedure ParseXMLThreaded; | |
begin | |
TThread.CreateAnonymousThread(procedure | |
begin | |
CoInitialize(nil); | |
try | |
ParseXML; | |
sleep(5000); // simulo carga de trabajo | |
finally | |
CoUninitialize; | |
end; | |
writeln('Final thread'); //no se debería hacer esto...en VCL usar Syncrhonize! | |
end).Start; | |
end; | |
var i:integer; | |
begin | |
ReportMemoryLeaksOnShutdown := True; | |
CoInitialize(nil); | |
try | |
ParseXML; | |
writeln('Xml parseado'); | |
finally | |
CoUninitialize; | |
end; | |
// | |
for i := 0 to 10 do | |
ParseXMLThreaded; | |
writeln('Threads lanzados...'); | |
readln; | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment