java读写xml文件

来源:互联网 发布:彼得潘大叔 知乎 编辑:程序博客网 时间:2024/06/06 09:36

1.从url中读取返回的报文,保存到xml文件
public void queryCityAreaInfos() throws HttpException, IOException{
HttpClient client = new HttpClient();
client.getHostConfiguration().setProxy(“182.1.1.200”, 80);
//设置代理服务器地址和端口
//client.getHostConfiguration().setProxy(“proxy_host_addr”,proxy_port);
//使用GET方法,如果服务器需要通过HTTPS连接,那只需要将下面URL中的http换成https
HttpMethod method = new GetMethod(GET_CITYAREA_URL);
//使用POST方法
//HttpMethod method = new PostMethod(“http://java.sun.com“;);
client.executeMethod(method);
//打印服务器返回的状态
//System.out.println(method.getStatusLine());
//打印返回的信息
// System.out.println(method.getResponseBodyAsString());
//设置保存目录
String savepathStr= System.getProperty(“user.dir”);//取得用户所在目录
//QueryRedFridayDataTimer为当前类名,下面方法获取的路径为类编译的class所在目录
String savepathStr2= QueryRedFridayDataTimer.class.getResource(“/”).getPath();
FileOutputStream fo = new FileOutputStream(savepathStr+”\cityArea.xml”);
PrintStream so = new PrintStream(fo,true,”utf-8”);//关键是这句要用带指定编码格式的构造方法
so.println(method.getResponseBodyAsString());

   //释放连接   method.releaseConnection();  }

2.从本地xml解析到List或Map中
public void addCouponData2Context(){
//获取当前日期
String temp_str=”“;
Date dt = new Date();
//最后的aa表示“上午”或“下午” HH表示24小时制 如果换成hh表示12小时制
SimpleDateFormat sdf = new SimpleDateFormat(“yyyyMMdd”);
temp_str=sdf.format(dt);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try{
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(savepathStr+”\coupon_”+temp_str+”.xml”);
NodeList itemList = doc.getElementsByTagName(“item”);
System.out.println(“共有” + itemList.getLength() + “个item节点”);
List couponList = new ArrayList();
for (int i = 0; i < itemList.getLength(); i++)
{
Node item = itemList.item(i);
org.w3c.dom.Element elem = (org.w3c.dom.Element) item;
System.out.println(“name:” + elem.getAttribute(“name”));
Map couponInfoMap = new HashMap();
for (Node node = item.getFirstChild(); node != null; node = node.getNextSibling())
{

             if (node.getNodeType() == Node.ELEMENT_NODE && node.getChildNodes().getLength()>0)               {                   String name = node.getNodeName();                   String value = node.getFirstChild().getNodeValue();                   System.out.print(name + ":" + value + "\t");                   couponInfoMap.put(name, value);                 List merchantInfosList = new ArrayList();                 if(node.hasChildNodes()){                     NodeList merchantInfoList = node.getChildNodes();                     for (int j = 0; j < merchantInfoList.getLength(); j++) {                         Map merchantInfoMap = new HashMap();                         Node merchantIn = merchantInfoList.item(j);                         for (Node merchantInfo = merchantIn.getFirstChild(); merchantInfo != null; merchantInfo = merchantInfo.getNextSibling()){                             if (merchantInfo.getNodeType() == Node.ELEMENT_NODE && merchantInfo.getChildNodes().getLength()>0) {                                 String name2 = merchantInfo.getNodeName();                                   String value2 = merchantInfo.getFirstChild().getNodeValue();                                   merchantInfoMap.put(name2, value2);                                 System.out.print("------"+name2 + ":" + value2 + "\t");                             }                         }                         if(!merchantInfoMap.isEmpty()){                             merchantInfosList.add(merchantInfoMap);                         }                     }                 }                 //                 couponInfoMap.put("merchantInfoList", merchantInfosList);             }           }         couponList.add(couponInfoMap);     }       context.setData("couponList", couponList);     }catch (Exception e) {        // TODO: handle exception    }}

3.报文xml格式

0 0