0

I am doing yahoo API program where i am getting result as XML format and fetching values from that xml files using the rule name. But in my case the xml file looks like this.

<person>
  <first>Kiran</first>
  <first>sam</first>
  <url>www.aaaaaaaaddd.com</url>
  <last>Pai</last>
  <age>22</age>
  <url>www.ajsajha.com</url?
</person>
<person>
  <first>Kiran</first>
  <first>tom</first>
  <url>www.aaaaasdadd.com</url>
  <last>Pai</last>
  <age>22</age>
  <url>www.wqeqwjha.com</url>
</person>

In this i want to take values from the following rules : first, url, lat, age. But i want to take only one value from the rule first, url.

if(e.getElementsByTag("url")!=null)
              {
                  itemStringBuilder.append(e.getElementsByTag("url").text());

              }

when i use this code i am getting both the url values. How can i check and avoid the second one and just take the first.

1
  • Are you using plain java DOM parser? or any thir party libraries? Because in java there is no method called "getElementsByTag" it is "getElementsByTagName" Commented Jun 16, 2014 at 6:43

1 Answer 1

2
Elements urls = e.getElementsByTag("url"):
if (urls != null)
{
    itemStringBuilder.append(urls.first().text());
}

Use first() on the Elements gotten. One call to get is more efficient.

Javadoc is your best friend.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.