Solved

searching a value from a string

Posted on 2013-01-25
3
184 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 27

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
String array comparison 4 57
How  can  i  resolve  HTTP Status 404 -? 8 80
ejb entity bean example issue 2 43
Print Rhino Java Array in Javascript 1 58
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

751 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