Solved

searching a value from a string

Posted on 2013-01-25
3
179 Views
Last Modified: 2013-01-30
hi guys
I have strings like this

String xmlfileLocation  = "";
string xmldata =
"<strong>Id:</strong> 57A3<br />
<strong>Type:</strong> Buy<br />
<strong>Amount:</strong> $28<br />
 <a href="http://www.bamboo.com/instit/R_0117_3.xml">XML version</a>
 
From the above string xmldata i want to extract the url value of xml file
so my final output would be
String xmlfileLocation = "http://www.bamboo.com/instit/R_0117_3.xml";
Any idea whats the best way to acchieve this ?

Shiould i convert the xmldata string to an XML object first?

any help will be greatly appreciated
thanks
0
Comment
Question by:royjayd
3 Comments
 
LVL 7

Expert Comment

by:Sumit Gupta
ID: 38820976
Hello,

Firstly it must be a well formed XML document. Meaning there be one root element defined and every tag is closed..

I don't know about Java. However, in PHP i would extract the value using the code snippet below :

$data ="<?xml version='1.0' encoding='utf-8'?>
    <root>
    <strong>Id:</strong><br>57A3</br>
    <strong>Type:</strong><br>Buy</br>
    <strong>Amount</strong><br>$28</br>
    <a href='http://www.bamboo.com/instit/R_0117_3.xml'>XML Versions</a>
</root>";
$xml = simplexml_load_string($data);
$children = $xml->xpath('/root/*');

foreach ($children as $test){
     if($test == "XML Versions"){
         $value = $test['href'];
         break;
     }
}  
echo 'The url under the link tag is'.$value;

The $value would essentially contain the url. Hope this helps!!!
0
 
LVL 26

Accepted Solution

by:
dpearson earned 500 total points
ID: 38821510
You can solve this by either
a) parsing the string as an XML document and then traversing the document there's a good example on how to do this here: http://www.java-samples.com/showtutorial.php?tutorialid=152

or in this case much more simply
b) just looking for the embedded tags you are interested in

            int start = xmldata.indexOf("a href=") ;
            int end = xmldata.indexOf(">", start) ;
            String href = xmldata.substring(start+8, end-1) ;

will return the URL from the first href tag in the document (or throw an exception if there is none).

Doug
0
 

Author Closing Comment

by:royjayd
ID: 38837480
that works, thanks doug
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now