0

How can I access the data inside this xml document using jsoup?

Specifically the F data, i.e F="11"

With what I have tried I only get the 900, 1080, 1260, 0, 180, 360 etc data returned.

<DV dataDate="2013-11-11T20:00:00Z" type="Forecast">
<Location i="3604" lat="51.708" lon="-5.055" name="MILFORD HAVEN C.B." country="WALES" continent="EUROPE">
<Period type="Day" value="2013-11-11Z">
<Rep D="W" F="11" G="18" H="100" Pp="15" S="9" T="13" V="MO" W="8" U="1">900</Rep>
<Rep D="W" F="12" G="18" H="100" Pp="52" S="7" T="13" V="VP" W="11" U="0">1080</Rep>
<Rep D="NNW" F="11" G="9" H="96" Pp="59" S="4" T="12" V="VG" W="12" U="0">1260</Rep>
</Period>
<Period type="Day" value="2013-11-12Z">
<Rep D="NW" F="9" G="13" H="88" Pp="80" S="7" T="11" V="VG" W="15" U="0">0</Rep>
<Rep D="NW" F="9" G="13" H="88" Pp="13" S="9" T="11" V="VG" W="7" U="0">180</Rep>
<Rep D="NNW" F="7" G="18" H="86" Pp="2" S="9" T="9" V="VG" W="0" U="0">360</Rep>
<Rep D="NNW" F="7" G="20" H="83" Pp="0" S="11" T="10" V="VG" W="1" U="1">540</Rep>

Thanks for your help.

1 Answer 1

2

First parse your xml:

String xml = "<DV dataDate='2013-11-11T20:00:00Z'....";
Document doc = Jsoup.parse(xml, "", Parser.xmlParser());

Then you can essentially traverse through the document as you would with a html document. For example, if you want to select all of the <rep> tags and print the F data:

Elements rep = doc.select("rep");
for (Element e : rep) {
    System.out.println(e.attr("f"));
}

Will output:

11
12
11
9
9
7
7

To select only the 4th F value: rep.get(3).attr("f") which will be 9.


Update:

You could just get the remote file as a String first and then use my above code as normal. For example:

String xml = Jsoup.connect("http://datapoint.metoffice.gov.uk/public/data/val/wxfcs/all/xml/3604?res=3hourly&key=2dd5950b-91e1-4671-9d83-625f2ae9cbf5").get().toString();

Document doc = Jsoup.parse(xml, "", Parser.xmlParser());
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your answer, will try it out. Also how would I select say the 4th F data?
@J4C3N-14 No problem. See my updated answer for selecting the 4th F data.
Hi just trying your answer, what if I don't have the file locally and connecting like this: docWeather2 = Jsoup.connect("datapoint.metoffice.gov.uk/public/data/val/wxfcs/all/xml/…; would I need to put the document into a file before attempting to parse it or is the a way like....jsoup.connect.xmlParser("blah,blah,blah")? Thanks
Thank you very, much it is exactly what I meant. Stackoverflow and users like you are a godsend, Thank you for your help.

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.