srikotesh
asked on
how to get the tag value from the string
Hi Experts,
String msg = "<notify:Header><notify:uu id>c364a21 43e1f4fb58 a87b1530c5 007cd</not ify:uuid>< notify:Sys temCode>DT P</notify: SystemCode ><notify:c ountryCode >AE</notif y:countryC ode><notif y:Identifi er>SYSTEM< /notify:Id entifier>< /notify:He ader>";
how to get the notify:uuid tag value from the above string
String value will not be static but it will have same tag notify:uuid
String msg = "<notify:Header><notify:uu
how to get the notify:uuid tag value from the above string
String value will not be static but it will have same tag notify:uuid
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi CEHJ,
Is there any other apporach.
it is taking 17 milliseconds time to get the value.
this operation i am doing on huge data.
Is there any other apporach.
it is taking 17 milliseconds time to get the value.
this operation i am doing on huge data.
I wish you'd said that earlier. Use an xml pull parser
https://en.wikipedia.org/wiki/StAX
https://en.wikipedia.org/wiki/StAX
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If speed is a concern and message always start like this:
<notify:Header><notify:uui d>
then I would update dpearson's code like this:
<notify:Header><notify:uui
then I would update dpearson's code like this:
private String getField(String msg) {
string result="";
int i=28;
while(msg[i]!='<')
result+=msg[i];
return result;
}
String uuid = getField(msg);
then I would update dpearson's code like this:No - string concatenation should be avoided - it's inefficient. If you don't know why, decompile the code that is produced by it and look how StringBuilder/StringBuffer
Of course, this question begs other, such as: why is an xml document, which is apparently massive, being held in memory (in a String) in the first place if the use case is to obtain one value from it?
ASKER
Hi CEHJ,
xml file I am recieving from the MQ queue,after recieving from the queue converting it as String.
From this i need only one tag value.
xml file I am recieving from the MQ queue,after recieving from the queue converting it as String.
From this i need only one tag value.
ASKER
thanks
:)
xml file I am recieving from the MQ queue,after recieving from the queue converting it as String.There might be a better way of doing that
You only needed to splice out the id, like this :
(100,000 iterations of that (without the System.println() obviously), took 31 ms on my machine).
System.out.println(msg.substring(msg.indexOf("d>")+2,msg.indexOf("</")));
(100,000 iterations of that (without the System.println() obviously), took 31 ms on my machine).
getField(String msg) takes less then 1ms to execute.
while this: msg = msg.replaceAll(".*<notify: uuid>(.*?) </notify:u uid>.*", "$1");
takes 13 ms.
here is the test code:
while this: msg = msg.replaceAll(".*<notify:
takes 13 ms.
here is the test code:
class test{
public static String getField(String msg) {
int i=28;
char[] msgArray = msg.toCharArray();
String result ="";
while(msgArray[i]!='<')
{
result+=msgArray[i];
i++;
}
return result;
}
public static void main(String[] args) {
long startTime = System.nanoTime();
String msg = "<notify:Header><notify:uuid>c364a2143e1f4fb58a87b1530c5007cd</notify:uuid><notify:SystemCode>DTP</notify:SystemCode><notify:countryCode>AE</notify:countryCode><notify:Identifier>SYSTEM</notify:Identifier></notify:Header>";
msg = msg.replaceAll(".*<notify:uuid>(.*?)</notify:uuid>.*", "$1");
//String uuid = getField(msg);
System.out.println(msg);
// System.out.println(uuid);
long endTime = System.nanoTime();
System.out.println("Took "+(endTime-startTime)/1000000+" ms to run.");
}
}
Just in case there's any doubt, my code is not supplied as optimal resource/efficiency - wise (since i was not aware that the input was large). It was supplied for its ease of use
getField(String msg) takes less then 1ms to execute.
while this: msg = msg.replaceAll(".*<notify:uuid>(.*?) </notify:u uid>.*", "$1");
takes 13 ms.
We know all about that. That’s why CEHJ and Doug spoke about it already.
function get_string($string, $start, $end){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}