加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml解析

发布时间:2020-12-16 00:05:13 所属栏目:百科 来源:网络整理
导读:http://www.cbex.com.cn/unpub/bj.xml http://www.cquae.com/unpub/pros.xml 第一种 Document doco; DocumentBuilderFactory dbfo = DocumentBuilderFactory.newInstance(); dbfo.setCoalescing(true); DocumentBuilder dbyo = dbfo.newDocumentBuilder(); d

http://www.cbex.com.cn/unpub/bj.xml

http://www.cquae.com/unpub/pros.xml

第一种

Document doco;

DocumentBuilderFactory dbfo = DocumentBuilderFactory.newInstance();
dbfo.setCoalescing(true);
DocumentBuilder dbyo = dbfo.newDocumentBuilder();
doco = dbyo.parse(url);

NodeList nlo = doco.getElementsByTagName("pubPrice");
NodeList nllo = doco.getElementsByTagName("title");
NodeList n23o = doco.getElementsByTagName("link");
NodeList pubDate = doco.getElementsByTagName("pubDate");
NodeList proNo = doco.getElementsByTagName("proNo");
int numgp = 0;
if ((numgp++) < 3) {
if (nlo.getLength() > 0) {
for (int j = 0; j < nlo.getLength(); j++) {
String str1 = nlo.item(j).getFirstChild().getNodeValue();
Double str22 = Double.valueOf(str1);
DecimalFormat format = new DecimalFormat("0.000000");
String s = format.format(str22);
int xo = j + 1;
String str2 = n23o.item(xo).getFirstChild().getNodeValue();
int xy = j + 1;
String str3 = nllo.item(xy).getFirstChild().getNodeValue();
String sj = pubDate.item(j).getFirstChild().getNodeValue();
String bh = proNo.item(j).getFirstChild().getNodeValue();


Map<String,String> map = new HashMap<String,String>();
map.put("xmbh",bh);
map.put("name",str3);
map.put("href",str2);
map.put("price",s);
map.put("date",sj);


list.add(map);
}
}
}





第二种

Document doco;

StringBuffer result = new StringBuffer("");
URL connect = new URL(url.toString());
URLConnection connection = connect.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
connection.getInputStream();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
result.append("rn");
}


StringReader sr = new StringReader(result.toString());
InputSource is = new InputSource(sr);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
doco = builder.parse(is);
line = null;
sr.close();


NodeList nlo = doco.getElementsByTagName("pubPrice"); NodeList nllo = doco.getElementsByTagName("title"); NodeList n23o = doco.getElementsByTagName("link"); NodeList pubDate = doco.getElementsByTagName("pubDate"); NodeList proNo = doco.getElementsByTagName("proNo"); int numgp = 0; if ((numgp++) < 3) { if (nlo.getLength() > 0) { for (int j = 0; j < nlo.getLength(); j++) { String str1 = nlo.item(j).getFirstChild().getNodeValue(); Double str22 = Double.valueOf(str1); DecimalFormat format = new DecimalFormat("0.000000"); String s = format.format(str22); int xo = j + 1; String str2 = n23o.item(xo).getFirstChild().getNodeValue(); int xy = j + 1; String str3 = nllo.item(xy).getFirstChild().getNodeValue(); String sj = pubDate.item(j).getFirstChild().getNodeValue(); String bh = proNo.item(j).getFirstChild().getNodeValue(); Map<String,String>(); map.put("xmbh",bh); map.put("name",str3); map.put("href",str2); map.put("price",s); map.put("date",sj); list.add(map); } } }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读