-4

I ma new to java. I have a string s:

s="<name>header</name><content>Good Morning</content>"

How to get value of content and name using Jsoup? What JSoup returns when content is empty?null?

user2511713
  • 555
  • 3
  • 7
  • 18

2 Answers2

1

You can use jsoup:

        Connection con2=Jsoup.connect(url);
        Document doc = con2.get();
        //or use Document doc = Jsoup.parse(html);
        Element e=doc.head().select("meta[name=header]").first();
        String url=e.attr("content");

http://jsoup.org/cookbook/extracting-data/attributes-text-html

http://jsoup.org/cookbook/extracting-data/selector-syntax

For your edit I do agree with the answer that @Hein give you.

surfealokesea
  • 4,971
  • 4
  • 28
  • 38
0

Use this RegEx for example: name=(.*) content=(.*) \/>. The name will be in the first group and the content in the second.

I would recommend double quotes around the strings though. In that case you can use this regex: name=\"(.*)\" content=\"(.*)\"

Edit after the OPs edit:

If you have complete control of the data yourself you should consider saving the name and content in seperate columns in your database, or look into serialization maybe.

Hein Andre Grønnestad
  • 6,885
  • 2
  • 31
  • 43
  • 1
    +1 for the simple and good solution to the malformed markup, -1 for suggesting the same approach for a well-formed one (if you have valid markup, use a proper parsing library, [not regexes](http://stackoverflow.com/a/1732454/520779)) – mgibsonbr Jul 18 '13 at 06:58
  • @mgibsonbr You're probably right, but you **could** use regex too, then you wouldn't have to depend on another library for such a simple case. There are multiple solutions, this is just one. – Hein Andre Grønnestad Jul 18 '13 at 07:15
  • Sure, I agree sometimes a quick and dirty solution might be the most sensible thing to do, especially if it's an one-off task. – mgibsonbr Jul 18 '13 at 07:24