Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

searching a value from a string

Posted on 2013-01-25
3
Medium Priority
?
187 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 8

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 28

Accepted Solution

by:
dpearson earned 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

877 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