0

I need to find out the user's country, tried via geoplugin.net , but there is xml code, how to read it I do not know.

URL oracle = new URL("http://www.geoplugin.net/xml.gp?ip=xx.xx.xx.xx");
        BufferedReader in = new BufferedReader(
                new InputStreamReader(oracle.openStream()));

        String inputLine;
        while ((inputLine = in.readLine()) != null)
            System.out.println(inputLine); 
        in.close();

This code outputs

<?xml version="1.0" encoding="UTF-8"?>
<geoPlugin>
    <geoplugin_request>91.xxx.118.xx</geoplugin_request>
    <geoplugin_status>206</geoplugin_status>
    <geoplugin_delay>1ms</geoplugin_delay>
    <geoplugin_credit>Some of the returned data includes GeoLite data created by MaxMind, available from &lt;a href='http://www.maxmind.com'&gt;http://www.maxmind.com&lt;/a&gt;.</geoplugin_credit>
    <geoplugin_city></geoplugin_city>
    <geoplugin_region></geoplugin_region>
    <geoplugin_regionCode></geoplugin_regionCode>
    <geoplugin_regionName></geoplugin_regionName>
    <geoplugin_areaCode></geoplugin_areaCode>
    <geoplugin_dmaCode></geoplugin_dmaCode>
    <geoplugin_countryCode>RU</geoplugin_countryCode>
    <geoplugin_countryName>Russia</geoplugin_countryName>
    <geoplugin_inEU>0</geoplugin_inEU>
    <geoplugin_euVATrate></geoplugin_euVATrate>
    <geoplugin_continentCode>EU</geoplugin_continentCode>
    <geoplugin_continentName>Europe</geoplugin_continentName>
    <geoplugin_latitude>55.7386</geoplugin_latitude>
    <geoplugin_longitude>37.6068</geoplugin_longitude>
    <geoplugin_locationAccuracyRadius>1000</geoplugin_locationAccuracyRadius>
    <geoplugin_timezone>Europe/Moscow</geoplugin_timezone>
    <geoplugin_currencyCode>RUB</geoplugin_currencyCode>
    <geoplugin_currencySymbol>&amp;#1088;&amp;#1091;&amp;#1073;</geoplugin_currencySymbol>
    <geoplugin_currencySymbol_UTF8>руб</geoplugin_currencySymbol_UTF8>
    <geoplugin_currencyConverter>66.0003</geoplugin_currencyConverter>
</geoPlugin>

I need to get the contents of the string <geoplugin_countryName>Russia</geoplugin_countryName>

  • 1
    Are you using/preferring any libraries for XML parsing? – dan1st Jan 15 '23 at 16:17
  • I don't use anything now, but I don't want to increase the weight of the source file with other libraries – Destroy Jan 15 '23 at 16:22
  • Theoretically, you _could_ just check for lines containing `` if you don't want to use any XML processing libraries. – dan1st Jan 15 '23 at 16:26
  • I'm trying to do it now – Destroy Jan 15 '23 at 16:28
  • Don't waste your time with such half-baked solutions. If you have XML then use a decent XML parser to extract information. In Java the XML libraries are part of the standard library. 99.9% of the time, trying to process XML with anything other than an XML parser will result in incorrect code. And for the remaining 0.1%, you have to know exactly what you're doing. – vanje Jan 15 '23 at 21:38

1 Answers1

-1
public static void main(String[] args) throws IOException {
    List<String> list = new ArrayList<>();
    URL oracle = new URL("http://www.geoplugin.net/xml.gp?ip=xx.xx.xx.xx");
    BufferedReader in = new BufferedReader(
            new InputStreamReader(oracle.openStream()));

    String inputLine;
    while ((inputLine = in.readLine()) != null)
        list.add(inputLine);

    String s = list.get(13).replace("\t<geoplugin_countryName>", "").replace("</geoplugin_countryName>", "");
    System.out.println(s);
    in.close();
}

this solved my problem

  • 2
    Yea .... not a good solution. For example, what happens if they modify their XML schema to add or remove or reorder the child elements of the `` element? You are better off using an XML parser. – Stephen C Jan 15 '23 at 16:38
  • 1
    I have closed this as a dup of a Q&A with some better answers. (Better in the sense that they shouldn't break when there is an upwards compatible change to the XML.) – Stephen C Jan 15 '23 at 16:41