解析HTML

GuoGongZhou 2009-12-17 09:48:59
上面给我个任务 叫我解析整个html页面 这个HTML包含各城市的天气情况 我要拿到这些个城市的天气
高手支招 O(∩_∩)O谢谢
...全文
79 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuyang840117 2009-12-17
  • 打赏
  • 举报
回复
代码才是硬道理 拿去参考吧
写的不是很共通


package cn.ccb.common.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ReadXml {

private Document getDocument(String xmlUrl) {
Document document = null;
SAXReader saxReader = new SAXReader();
FileInputStream file;
try {
file = new FileInputStream(xmlUrl);
InputStreamReader reader = new InputStreamReader(file, "UTF-8");
document = saxReader.read(reader);
} catch (FileNotFoundException e) {

e.printStackTrace();
} catch (UnsupportedEncodingException e) {

e.printStackTrace();
} catch (DocumentException e) {

e.printStackTrace();
}

// SAXReader reader = new SAXReader();
// Document document = null;
// try {
// document = reader.read(new File(xmlUrl));
// } catch (DocumentException e) {
// e.printStackTrace();
// } catch (MalformedURLException e) {
// e.printStackTrace();
// }
return document;
}

/**
* 获取当前步骤号对应的部门ID
*
* @param xmlUrl
* 获取XML地址
* @param step
* 步骤号
* @return 部门ID
*/
public String getRole(String xmlUrl, String step) {
Document document = this.getDocument(xmlUrl);
Element elt = (Element) document.selectSingleNode("//step[@id='" + step
+ "']/receiver");
return elt.attributeValue("role");
}

/**
* 通过当前step查询下一步对应的角色
*
* @param xmlUrl
* @param step
* @return
*/
public String getNextRole(String xmlUrl, String step) {
Document document = this.getDocument(xmlUrl);
Element elt = (Element) document.selectSingleNode("//step[@id='" + step
+ "']/receiver/target/choice/sender");
return elt.attributeValue("role");
}

/**
* 通过当前step查询下一步对应的step
*
* @param xmlUrl
* @param step
* @return
*/
public String getNextStep(String xmlUrl, String step) {
Document document = this.getDocument(xmlUrl);
Element elt = (Element) document.selectSingleNode("//step[@id='" + step
+ "']/receiver/target/choice/sender");
return elt.attributeValue("step");
}

/**
* 获取下一步发送部门数组
*
* @param xmlUrl
* XML地址
* @param step
* 当前步骤号
* @param node
* choice or split
* @return Map<role,step>
*/
public Map getNextStepItems(String xmlUrl, String step, String node) {
Document document = this.getDocument(xmlUrl);
List list = document.selectNodes("//step[@id='" + step
+ "']/receiver/target/" + node + "/sender");
Map map = new HashMap();
Element elt;
if (null != list) {
for (int i = 0; i < list.size(); i++) {
elt = (Element) list.get(i);
map.put(elt.attributeValue("role"), elt.attributeValue("step"));
}
}
return map;
}

/**
* 通过角色ID获取步骤号
*
* @param xmlUrl
* @param role
* 角色ID
* @return 步骤号
*/
public String getStepByRole(String xmlUrl, String role) {
Document document = this.getDocument(xmlUrl);
List list = document.selectNodes("//step");
String step = "";
Element elt;
if (null != list) {
for (int i = 0; i < list.size(); i++) {
elt = (Element) list.get(i);
if (role.equals(elt.element("receiver").attribute("role"))) {
step = elt.attributeValue("id");
break;
}
}
}
return step;
}

/**
* 获取当前步骤需要的直线数
*
* @param xmlUrl
* @param step
* @return
*/
public String getNeedStepNum(String xmlUrl, String step) {
Document document = this.getDocument(xmlUrl);
List list = document.selectNodes("//step[@id='" + step
+ "']/receiver/join/target-from");
Element elt;
String str = "";
if (null == list || 0 == list.size()) {
return "";
} else {
elt = (Element) list.get(0);
str = elt.attributeValue("step");
for (int i = 1; i < list.size(); i++) {
elt = (Element) list.get(i);
str = str + "," + elt.attributeValue("step");
}
return str;
}
}

public String getDescriptionByStep(String xmlUrl, String step) {
Document document = this.getDocument(xmlUrl);
Element elt = (Element) document.selectSingleNode("//step[@id='" + step
+ "']");
return elt.attributeValue("description");
}
}
xuqi2850661 2009-12-17
  • 打赏
  • 举报
回复
试试dom4j
GuoGongZhou 2009-12-17
  • 打赏
  • 举报
回复
不要沉了 自己顶起
GuoGongZhou 2009-12-17
  • 打赏
  • 举报
回复
用dom 解析可以吗
GuoGongZhou 2009-12-17
  • 打赏
  • 举报
回复
各位 要是有 稳定的web服务(最好免费的)也提供一下
GuoGongZhou 2009-12-17
  • 打赏
  • 举报
回复
不要沉了 自己顶起

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧