Most efficient way to grab XML tag from file with JavaScript and Regex -


i'm doing more advanced automation on ios devices , simulators enterprise application. automation written in browserless javascript. 1 of methods works on device not on simulator, need code workaround. curious, it's uiatarget.localtarget().frontmostapp().preferencesvalueforkey(key).

what need read path server (which varies) plist file on disk. workaround on simulator, i've used following lines locate plist file containing preferences:

// alias of user who's logged in var result = uiatarget.localtarget().host().performtaskwithpathargumentstimeout("/usr/bin/whoami", [], 5).stdout;  // remove newline @ end of alias got result = result.replace('\n',"");  // find location of plist containing server info result = uiatarget.localtarget().host().performtaskwithpathargumentstimeout("/usr/bin/find", ["/users/"+result+"/library/application support/iphone simulator", "-name", "redacted.plist"], 100);  // reason need delay here uiatarget.localtarget().delay(.5);  // results returned in single string separated newline characters, can split array // array contains of folders have plist file under simulator directory var plistlocations = result.stdout.split("\n");  ...  // example, let's assume want slot 0 here save time var plistbinarylocation = plistlocations[0]; var plistxmllocation =  plistlocations[i] + ".xml"; result = uiatarget.localtarget().host().performtaskwithpathargumentstimeout("/usr/bin/plutil", ["-convert","xml1", plistbinarylocation,"-o", plistxmllocation], 100); 

from here, think best way contents cat or grep file, since can't read file directly disk. however, i'm having trouble getting syntax down. here's edited snippet of plist file i'm reading:

<key>server_url</key> <string>http://pathtoserver</string> 

there bunch of key/string pairs in file, server_url key unique. ideally i'd lookback, because javascript doesn't appear support it, figured i'd pair file , whittle down bit later.

i can search key this:

// line works var expression = new regexp(escaperegexp("<key>server_url</key>"));  if(result.stdout.match(expression)) {     uialogger.logmessage("found it!!!"); } else {     uialogger.logmessage("nope :("); } 

where escaperegexp method looks this:

function escaperegexp(str)  {     var result =  str.replace(/([()[{*+.$^\\|?])/g, '\\$1');      uialogger.logmessage("new string: " + result);     return result; } 

also, line returns value (but gets wrong line):

var expression = new regexp(escaperegexp("<string>(.*?)</string>")); 

however, when put 2 together, (the regex syntax) works on terminal doesn't work in code:

var expression = new regexp(escaperegexp("<key>server_url</key>[\s]*<string>(.*?)</string>")); 

what missing? tried grep , egrep without luck.

there 2 problems affecting here getting regex work in javascript code.

  • first, escaping whole regex expression string, means capturing (.*?) , whitespace ignoring [\s]* escaped , won't evaluated way you're expecting. need escape xml parts , add in regex parts without escaping them.
  • second, whitespace ignoring part, [\s]* falling prey javascript's normal string escaping rules. "\s" turning "s" in output. need escape backslash "\s" stays "\s" in string pass construct regular expression.

i've built working script i've verified in ui automation engine itself. should extract , print out expected url:

var teststring = "" + "<plistexample>\n" + "   <key>dont-find-me</key>\n" + "   <string>bad value</string>\n" + "   <key>server_url</key>\n" + "   <string>http://server_url</string>\n" + "</plistexample>";  function escaperegexp(str)  {     var result =  str.replace(/([()[{*+.$^\\|?])/g, '\\$1');      uialogger.logmessage("new string: " + result);     return result; }  var strexp = escaperegexp("<key>server_url</key>") + "[\\s]*" + escaperegexp("<string>") + "(.*)" + escaperegexp("</string>");  uialogger.logmessage("expression escaping xml parts:" + strexp);  var exp = new regexp(strexp); var match = teststring.match(exp);  uialogger.logmessage("match: " + match[1]); 

i should point out, though, thing need escape in regex forward slashes in xml closing tags. means don't need escaperegexp() function , can write expression want this:

var exp = new regexp("<key>server_url<\/key>[\\s]*<string>(.*)<\/string>"); 

Comments