解析在线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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明
在Java中解析在线XML可以使用Java中内置的XML解析器,例如DOM、SAX或StAX。 以下是使用DOM解析解析在线XML的示例代码: ```java import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import org.w3c.dom.Element; import java.net.URL; public class XMLParser { public static void main(String argv[]) { try { URL url = new URL("http://www.example.com/data.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(url.openStream()); doc.getDocumentElement().normalize(); System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); NodeList nList = doc.getElementsByTagName("item"); System.out.println("-----------------------"); for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); System.out.println("\nCurrent Element :" + nNode.getNodeName()); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; System.out.println("Title : " + eElement.getElementsByTagName("title").item(0).getTextContent()); System.out.println("Link : " + eElement.getElementsByTagName("link").item(0).getTextContent()); System.out.println("Description : " + eElement.getElementsByTagName("description").item(0).getTextContent()); } } } catch (Exception e) { e.printStackTrace(); } } } ``` 在上面的示例中,我们使用Java中的URL类创建了一个URL对象来访问在线XML文件,然后使用DocumentBuilderFactory和DocumentBuilder类创建了一个DOM解析器对象,使用该对象解析XML文件。 我们使用doc.getDocumentElement().normalize()方法规范化XML文档,然后使用getElementsByTagName()方法获取所有元素,并使用for循环遍历这些元素。在循环中,我们使用Node对象的getNodeType()方法检查当前节点的类型,如果是一个元素节点,则使用Element对象获取元素的信息。 这只是使用DOM解析器的基本示例,您可以根据您的需要进行更改和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值