Link to home
Start Free TrialLog in
Avatar of srikotesh
srikotesh

asked on

how to get the tag value from the string

Hi Experts,

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>";

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
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Anwar Saiah
Anwar Saiah

In php you can use this script

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);
}
Avatar of srikotesh

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.
I wish you'd said that earlier. Use an xml pull parser
https://en.wikipedia.org/wiki/StAX
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If speed is a concern and message always start like this:
<notify:Header><notify:uuid>
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);

Open in new window

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 works in Java

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?
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.
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 :

System.out.println(msg.substring(msg.indexOf("d>")+2,msg.indexOf("</")));

Open in new window


(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:uuid>.*", "$1");
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.");
   }
   
   }

Open in new window

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:uuid>.*", "$1");
takes 13 ms.

We know all about that. That’s why CEHJ and Doug spoke about it already.