Survey 123 using the result of a pulldata via regex to record rainfall where null values are valid

858
2
09-05-2021 10:33 PM
MathieuBoonen
Occasional Contributor

In a json pulldata response I get a result that looks like this:

<?xml version="1.0" ?>

<Hilltop>

<Agency>Gisborne District Council</Agency>

<Measurement SiteName="Arowhana Repeater">

<DataSource Name="Rainfall" NumItems="1">

<TSType>StdSeries</TSType>

<DataType>SimpleTimeSeries</DataType>

<Interpolation>Histogram</Interpolation>

<ItemInfo ItemNumber="1">

<ItemName>Rainfall</ItemName><ItemFormat>F</ItemFormat><Units>mm</Units><Format>#.###</Format>

</ItemInfo>

</DataSource>

<Data DateFormat="Calendar" NumItems="1">

<E><T>2021-09-06T16:40:00</T><I1>23.2</I1></E>

</Data>

</Measurement>

</Hilltop>

All I want from the data is the rainfall value  in this case 23.2

The pulldata works fine but I try to use regex to extract out the rainfall value (also where ' ') can be a valid response from the server

I have tried substr but that has limited success when the response length is inconsistent

I tried to use regex(${hilltop_json},'^[+-]?[0-9]{1,2}(\.[05]{1})?$')  against the pulldata json response above but I just get a 'false' as the response

the response is always between the <I1> and </I1> html placeholders (the api is not that clean to present the data that  java script  stringify and parse seems to recognise unless it is in this html format as per above.)

What would be a more effective way to extract this value as depending on the returned value (amount of rain) ? I wish the survey user to do a specific task differently when the rainfall increases.

0 Kudos
2 Replies
Ruth_JiatengXu
Esri Contributor

Hi @MathieuBoonen 

Thanks for sharing here! I think you could write another JavaScript function to parse the XML to an object, and then you can select the  node you want to export.

Here is the blog on custom JS functions: https://community.esri.com/t5/arcgis-survey123-blog/extending-survey123-smart-forms-with-custom-js/b...

Here is the sample JS function to parse the XML.

 

 

myXML = '<?xml version="1.0" ?><Hilltop><Agency>Gisborne District Council</Agency><Measurement SiteName="Arowhana Repeater"><DataSource Name="Rainfall" NumItems="1"><TSType>StdSeries</TSType><DataType>SimpleTimeSeries</DataType><Interpolation>Histogram</Interpolation><ItemInfo ItemNumber="1"><ItemName>Rainfall</ItemName><ItemFormat>F</ItemFormat><Units>mm</Units><Format>#.###</Format></ItemInfo></DataSource><Data DateFormat="Calendar" NumItems="1"><E><T>2021-09-06T16:40:00</T><I1>23.2</I1></E></Data></Measurement></Hilltop>';

parse = new DOMParse();

xmlDoc = parser.parseFromString(myXML, "text/xml");

xmlDoc.getElementsByTagName("I1")[0].childNodes[0].nodeValue;

 

 

Thanks,

Ruth

0 Kudos
MathieuBoonen
Occasional Contributor

Hi @Ruth_JiatengXu  or anyone else with the smarts to make this work

Is there an alternative to using DOM Objects as apparently DOM does not work with Survey123 according to the custom JS functions Blog reference that you provided, or has things changed and I just cant get your script to work with the dynamic data string produced?

0 Kudos