.xml格式获取返回数据(ie 谷歌)

 
$.post(url, function (xml) {
        var obtainInfo;
        //判断是否是ie浏览器
        if( isIE() ){
            var str = xml.lastChild;
            obtainInfo = JSON.parse(str ["textContent"]);
        }else{
            var str = $(xml)[0]["lastElementChild"]["innerHTML"];
            obtainInfo = JSON.parse(str);
        }

判断是否是IE
 
function isIE() {
    return window.ActiveXObject || "ActiveXObject" in window ? !0 : !1
}


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 HttpURLConnection 获取网络数据可以支持 XML 格式返回和 JSON 格式返回。 对于 XML 格式返回,可以使用 Java 内置的 DOM 或 SAX 解析器来解析 XML 数据。下面是一个示例代码: ```java URL url = new URL("http://example.com/data.xml"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept", "application/xml"); InputStream stream = conn.getInputStream(); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(stream); // 对 XML 数据进行解析,例如: NodeList nodeList = doc.getElementsByTagName("item"); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); String title = node.getAttributes().getNamedItem("title").getNodeValue(); // ... } ``` 对于 JSON 格式返回,可以使用第三方库,例如 Google 的 Gson、Apache 的 Jackson 等来解析 JSON 数据。下面是一个示例代码: ```java URL url = new URL("http://example.com/data.json"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept", "application/json"); InputStream stream = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); StringBuilder response = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { response.append(line); } Gson gson = new Gson(); JsonObject jsonObject = gson.fromJson(response.toString(), JsonObject.class); // 对 JSON 数据进行解析,例如: JsonArray jsonArray = jsonObject.getAsJsonArray("items"); for (JsonElement element : jsonArray) { JsonObject item = element.getAsJsonObject(); String title = item.get("title").getAsString(); // ... } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值