You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java读取网站XML数据并提取geoplugin_countryName字段方法

解析GeoPlugin XML获取国家名称的Java实现

方法一:使用Java标准DOM解析器

先修改原有代码,将XML内容拼接为完整字符串,再通过DOM解析提取目标字段:

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public class GeoPluginParser {
    public static void main(String[] args) {
        try {
            URL url = new URL("http://www.geoplugin.net/xml.gp?ip=xx.xx.xx.xx");
            // 读取并拼接完整XML内容
            StringBuilder xmlContent = new StringBuilder();
            InputStreamReader reader = new InputStreamReader(url.openStream(), StandardCharsets.UTF_8);
            int c;
            while ((c = reader.read()) != -1) {
                xmlContent.append((char) c);
            }
            reader.close();

            // 初始化DOM解析器
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(new ByteArrayInputStream(xmlContent.toString().getBytes(StandardCharsets.UTF_8)));

            // 提取国家名称
            NodeList countryNameNodes = doc.getElementsByTagName("geoplugin_countryName");
            if (countryNameNodes.getLength() > 0) {
                Element countryElement = (Element) countryNameNodes.item(0);
                String countryName = countryElement.getTextContent().trim();
                System.out.println("国家名称:" + countryName);
            } else {
                System.out.println("未找到国家名称字段");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

方法二:使用XPath简化解析

XPath可直接定位目标节点,代码更简洁:

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.InputStream;
import java.net.URL;

public class GeoPluginXPathParser {
    public static void main(String[] args) {
        try {
            URL url = new URL("http://www.geoplugin.net/xml.gp?ip=xx.xx.xx.xx");
            InputStream inputStream = url.openStream();

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(inputStream);

            XPath xpath = XPathFactory.newInstance().newXPath();
            String countryName = xpath.evaluate("/geoPlugin/geoplugin_countryName", doc).trim();
            System.out.println("国家名称:" + countryName);

            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

注意事项

  • 实际开发中务必处理网络、XML解析等异常,避免程序崩溃
  • XML结构固定时,XPath方式代码更简洁易维护
  • 不推荐用正则表达式解析XML(结构变动时易失效),若临时使用可参考匹配规则:<geoplugin_countryName>(.*?)</geoplugin_countryName>

内容的提问来源于stack exchange,提问作者Destroy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 03:55:21