Extract XML/HTML Field

  • FH versions: V5, V6, and V7
  • Category: Web 

Function to extract a single text node value from an XML string. Note this version will only extract the first value if more than one exists.

It uses a Regular Expression pattern <node>(.-)</node> to locate the text node where:

<node> is a literal string node name prefix
( ) [parentheses] enclose the matching captured text node value
. [period] represents any single character
- [hyphen] is 0 or more repetitions of [period] for the shortest possible match
</node> is a literal string node name suffix


-- Get Text from XML Data using Node name
function StrGetXmlText(strData,strNode)
	local strPrefix = "<"..strNode..">"
	local strSuffix = "</"..strNode..">"
	local strRegExp = strPrefix.."(.-)"..strSuffix
	return strData:match(strRegExp)
end -- function StrGetXmlText
-- Get Text from XML Data using Node name (compact version)
function strGetXmlText(strData,strNode)
	return strData:match("<"..strNode..">(.-)</"..strNode..">")
end -- function strGetXmlText



strXML = [[
<ResultSet version="1.0">
strLat = StrGetXmlText(strXML,"latitude")
strLng = strGetXmlText(strXML,"longitude")
-- strLat = 38.898717
-- strLng = -77.035974 


Last update: 16 Dec 2020