PPWIZARD is a free preprocessor for HTML, REXX, Visual Basic or any text files.
\ -> Examples / Tips -> Loading XML into a DOM (in windows)
Loading XML into a DOM (in windows) |
;----------------------------------------------------------------------------
;--- Initialization code ----------------------------------------------------
;----------------------------------------------------------------------------
#NextId
#RexxVar @@XmlCleanupRequired = 'N'
#DefineRexx 'XmlStart'
;--- Load what file? ----------------------------------------------------
@@XmlFile = "{$FILE}";
;--- Load "w32util.dll" -------------------------------------------------
if RxFuncAdd('w32loadfuncs','w32util','w32loadfuncs') > 0 then
error('Could not load "w32util", reason: ' || RxFuncErrMsg());
call W32LoadFuncs;
@@XmlCleanupRequired = 'Y';
@@True = 1;
@@False = 0;
;--- Load the XML COM object --------------------------------------------
oXML = W32CreateObject('MSXML.DOMDocument')
if oXML = 0 | oXML = '' then
error('Could not create "MSXML.DOMDocument", reason: ' || W32OleGetError());
;--- Set some boolean properties ----------------------------------------
@@Ignore = w32PutProperty(oXML, "ValidateOnParse", "b", @@False)
@@PutRc = Rc
@@Ignore = w32PutProperty(oXML, "Async", "b", @@False)
@@PutRc = @@PutRc + Rc
@@Ignore = w32PutProperty(oXML, "ResolveExternals", "b", @@False)
@@PutRc = @@PutRc + Rc
if @@PutRc <> 0 then
error("Failed setting XML processing properties...");
;--- Load the file into the DOM -----------------------------------------
if FileQueryExists(@@XmlFile) = '' then
error('The XML file "' || @@XmlFile || '" doesn''t exist!');
@@Worked = W32CallFunc(oXML, "Load", "s", @@XmlFile);
if @@Worked = 0 then
error('Failed loading the XML file "' || @@XmlFile || '"',,'Reason: ' W32OleGetError());
#DefineRexx
;----------------------------------------------------------------------------
;--- Termination code -------------------------------------------------------
;----------------------------------------------------------------------------
#DefineRexx 'XmlCleanup'
if @@XmlCleanupRequired = 'Y' then
call W32OleCleanup
#DefineRexx
#DefineRexx ''
;--- Make sure we always do the cleanup code! ---------------------------
call ScheduleCleanupCode 'XmlCleanup';
#DefineRexx
;----------------------------------------------------------------------------
;--- Usefull macro ----------------------------------------------------------
;----------------------------------------------------------------------------
#DefineRexx 'XmlGetXpathValue'
@@Xpath = "{$XPATH}"
@@oXpath = w32getsubobj(oXML, "selectSingleNode", "s", @@Xpath)
if Rc = 1 then
error('Failed to find value for the XPATH "' || @@XPath || '"',, 'Reason: ' W32OleGetError());
{$#1} = W32GetProperty(@@oXpath, "text")
if w32GetProperty(@@oXpath, "nodeName") = "" then
error("XPATH FAILED: " || w32olegeterror())
#DefineRexx
;----------------------------------------------------------------------------
;--- Now Try the code -------------------------------------------------------
;----------------------------------------------------------------------------
#define TEST_XML_FILENAME out\TestXml.XML
#output "<$TEST_XML_FILENAME>" ASIS
;--- Create a simple XML file -------------------------------------------
<a>
<b>Value.B</b>
<c>Value.C</c>
</a>
#output
#DefineRexx ''
;--- Load the simple XML we created above -------------------------------
<$XmlStart FILE="<$TEST_XML_FILENAME>">
;--- Get two XML values ---------------------------------------------
<$XmlGetXpathValue "@@ValueB" XPATH="//a/b">
<$XmlGetXpathValue "@@ValueC" XPATH="//a/c">
;--- Display the values ---------------------------------------------
call Info "VALUE B: " || @@ValueB
call Info "VALUE C: " || @@ValueC
;--- Perform cleanup ----------------------------------------------------
<$XmlCleanup>
#DefineRexx
PPWIZARD Manual

Saturday May 28 2022 at 2:55pm