解析在线xml_如何解析xml文件

解析在线xml_如何解析xml文件private List> logicsidi(String url) { List> list = new ArrayList>(); try { Document doco; if("http://www.cquae.com/unpub/pros.xml".

解析在线xml_如何解析xml文件private List<Map<String, String>> logicsidi(String url) {


List<Map<String, String>> list = new ArrayList<Map<String, String>>();


try {


Document doco;


if(“http://www.cquae.com/unpub/pros.xml”.equals(url) || “http://www.cbex.com.cn/unpub/bj.xml”.equals(url)){


DocumentBuilderFactory dbfo = DocumentBuilderFactory.newInstance();


dbfo.setCoalescing(true);


DocumentBuilder dbyo = dbfo.newDocumentBuilder();


doco = dbyo.parse(url);


}else{


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(“\r\n”);


}


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 = 0.0;


String s = null;


try {


str22 = Double.valueOf(str1);


DecimalFormat format = new DecimalFormat(“0.000000”);


s = format.format(str22);


} catch (NumberFormatException e) {


e.printStackTrace();


s = str1;


}





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);


}


}


}





} catch (MalformedURLException e) {


e.printStackTrace();


return null;


} catch (UnsupportedEncodingException e) {


e.printStackTrace();


return null;


} catch (DOMException e) {


e.printStackTrace();


return null;


} catch (IOException e) {


e.printStackTrace();


return null;


} catch (ParserConfigurationException e) {


e.printStackTrace();


return null;


} catch (SAXException e) {


e.printStackTrace();


return null;


}


return list;


}今天的文章解析在线xml_如何解析xml文件分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/53233.html

(0)
编程小号编程小号
上一篇 2023-08-30 18:11
下一篇 2023-08-30 18:30

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注