ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Help / WLanguage / WLanguage functions / Standard functions / XML file functions
  • Example for interrogating the content of an XML document
  • Example for interrogating the content of an XML document
  • Example of search performed on the content of an attribute
XMLExecuteXPath (Example)
Example for interrogating the content of an XML document
Reports and QueriesWindows The XPATH query is used to retrieve 3 types of information at once = title, summary and url. Let's consider the query "//entry/title | //entry/summary | //entry/link"
XMLSource is string = [
<?xml version="1.0" encoding="ISO-8859-1"?>
<feed>
<author>Patrick</author>
<entry>
<title type='text'>A small moment of peace</title>
<summary type='text'>Simply the real life</summary>
<link type='application/atom+xml' href='http://somewhere/atom?authkey=WgHbgT990V8'></link>
<link type='text/html' href='http://somewhere?authkey=WgHbgT990V8'></link>
</entry>
<entry>
<title type='text'>Live in action</title>
<summary type='text'>After the roller blade fall (harmless thanks to protections)</summary>
<link type='application/atom+xml' href='http://somewhere/atom?authkey=Wg4sdf4ee'></link>
<link type='text/html' href='http://somewhere?authkey=Wg4sdf4ee'></link>
</entry>
</feed>
]
IF XMLDocument("XMLDoc", XMLSource) = True THEN
      XPATHQuery is string
      XPATHQuery = "//entry/title | //entry/summary | //entry/link"
XMLExecuteXPath("XMLDoc",  XPATHQuery) = True THEN
WHILE XMLFound("XMLDoc") = True
Trace("Title = " + XMLData("XMLDoc"))
XMLNext("XMLDoc")
Trace("Summary = " + XMLData("XMLDoc"))
XMLNext("XMLDoc")
WHILE XMLFound("XMLDoc") AND XMLElementName("XMLDoc") = "link"
IF XMLData("XMLDoc", "type") = "text/html" THEN
Trace("Url = " + XMLData("XMLDoc", "href"))
END
XMLNext("XMLDoc")
END
END
END
XMLClose("XMLDoc")
ELSE
Error(ErrorInfo())
END
Example for interrogating the content of an XML document
Reports and QueriesWindows The exploration is performed by XMLExecuteXPath.
In the example, the following elements will be retrieved in the XML document:
  • data found in the header,
  • data regarding the persons in the rest of the document.
The document has the following format, to run test of the example.
Create a text file containing this code, name it test.xml and store it in the \exe\ directory of your project.
<SERVICES>
<PARAMETERS>
<TYPE>AZ0256</TYPE>
<DATE>20070115</DATE>
</PARAMETERS>
<DATA>
<PERSONS>
<PERSON>
<TYPE>AB</TYPE>
<ID>156</ID>
<PREFERRED_NAME> </PREFERRED_NAME>
<FIRST_NAME>VINCE</FIRST_NAME>
<LAST_NAME>SMITH</LAST_NAME>
<GENDER>1</GENDER>
</PERSON>
<PERSON>
<TYPE>AC</TYPE>
<ID>246</ID>
<PREFERRED_NAME> </PREFERRED_NAME>
<FIRST_NAME>CLARA</FIRST_NAME>
<LAST_NAME>BARRET</LAST_NAME>
<GENDER>1</GENDER>
</PERSON>
<PERSON>
<TYPE>AB</TYPE>
<ID>247</ID>
<PREFERRED_NAME> </PREFERRED_NAME>
<FIRST_NAME>BEN</FIRST_NAME>
<LAST_NAME>RAMIREZ</LAST_NAME>
<GENDER>2</GENDER>
</PERSON>
<PERSON>
<TYPE>AB</TYPE>
<ID>248</ID>
<PREFERED_NAME>NATHAN</PREFERED_NAME>
<FIRST_NAME>ANA</FIRST_NAME>
<LAST_NAME>DESMOND</LAST_NAME>
<GENDER>3</GENDER>
</PERSON>
</PERSONS>
</DATA>
</SERVICES>
nDocumentPosition is int
XMLDocument("XMLDoc", fLoadText("D:\TESTS CUSTOMERS\2007-01\071109\test.xml"))
// Retrieve the type in the header of the document
XMLExecuteXPath("XMLDoc", "/SERVICES/PARAMETERS/TYPE")
Trace(XMLData("XMLDoc"))
// Retrieve the date in the header of the document
XMLExecuteXPath("XMLDoc", "/SERVICES/PARAMETERS/DATE")
Trace(DateToString(XMLData("XMLDoc")))
// Browse all the persons found in the document
XMLExecuteXPath("XMLDoc", "/SERVICES/DATA/PERSONS/PERSON")
WHILE XMLFound("XMLDoc") = True
Trace(" ")
Trace(XMLPosition("XMLDoc"))
nDocumentPosition = XMLSavePosition("XMLDoc")
XMLChild("XMLDoc")
WHILE NOT XMLOut("XMLDoc")
Trace(" >> "+XMLElementName("XMLDoc")+": " + XMLData("XMLDoc"))
XMLNext("XMLDoc")
END
XMLRestorePosition("XMLDoc", nDocumentPosition)
XMLNext("XMLDoc")
END
Example of search performed on the content of an attribute
Reports and QueriesWindows The search is performed by XMLExecuteXPath.
The purpose of the example is to retrieve, in a list of articles, all the attributes of the articles whose designation attribute contains "Screws":
sXMLSource is string  = [
<?xml version="1.0" encoding="ISO-8859-1"?>
<articles>
<art famcod="Demo Family" ref="789852">
<desi>Screw holders 1/2</desi>
</art>
<art famcod="Demo Family" ref="789465">
<desi>Test suitcase</desi>
</art>
<art famcod="Demo Family" ref="456987">
<desi>Stainless steel screws</desi>
</art>
<art famcod="Demo Family" ref="123654">
<desi>Snaphook</desi>
</art>
<art famcod="Demo Family" ref="789963">
<desi>Screws in bulk</desi>
</art>
<art famcod="Demo Family" ref="963258">
<desi>Titanium screws</desi>
</art>
</articles>
]
let sSearchVal = "Screws"
 
IF XMLDocument("XMLDoc", sXMLSource) = True THEN
 
XPATHQuery is string = "//art[contains(./desi,'"+sSearchVal+"')]"
 
XMLExecuteXPath("XMLDoc",  XPATHQuery) = True THEN
WHILE XMLFound("XMLDoc") = True
Trace("---------------------------------------------")
Trace(sSearchVal+" found >> "+XMLPosition("XMLDoc"))
Trace("Reference >> "+XMLData("XMLDoc","ref"))
 
XMLNext("XMLDoc")
END
// Browse completed, end the query
XMLCancelSearch("XMLDoc")
 
END
XMLClose("XMLDoc")
ELSE
Error(ErrorInfo())
END
Minimum version required
  • Version 9
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 05/26/2022

Send a report | Local help