Solved

searching a value from a string

Posted on 2013-01-25
3
185 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 28

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

615 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