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 |
Code
-
-- 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
Usage
-
strXML = [[ <ResultSet version="1.0"> <quality>85</quality> <latitude>38.898717</latitude> <longitude>-77.035974</longitude> <offsetlat>38.898590</offsetlat> <offsetlon>-77.035971</offsetlon> <latitude>52.0</latitude> <longitude>0.0</longitude> </ResultSet> ]] strLat = StrGetXmlText(strXML,"latitude") strLng = strGetXmlText(strXML,"longitude") print(strLat) print(strLng) -- strLat = 38.898717 -- strLng = -77.035974