閱讀564 返回首頁    go 人物


DOM4J解析XML

<?xml version="1.0" encoding="UTF-8"?>
<xsi:ADI xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance">
  <Reply>
    <Property Name="Result">0</Property>
    <Property Name="Description" />
  </Reply>
</xsi:ADI>



代碼如下:

/**
 * <project>hansci_cms</project>
 * <package>com.hansci.cms.webservice</package>
 * <class>C2WebService.java</class>
 * @time:2013-3-18 下午5:36:33
 *
 */
package com.hansci.cms.webservice;

import iptv.CSPResult;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.Iterator;
import java.util.Properties;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.hansci.base.util.PropertiesLoader;
import com.hansci.cms.util.C2Result;
import com.hansci.cms.util.FTPTool;

/**
 * @author zhuzhengke
 * 
 */
public class C2WebService implements Serializable {

	private static final long serialVersionUID = 1876014158844947767L;

	private Logger log = Logger.getLogger(C2WebService.class);
	
	private static final String HUAWEI_FTP_PORT ="HUAWEI_FTP_PORT";
	private static final String HANSCI_REMOTE_XML_FILE_PATH = "HANSCI_REMOTE_XML_FILE_PATH";
	private static final String HUAWEI_TITLE_PATH = "HUAWEI_TITLE_PATH";
	
	/***
	 * 這個webservice用於讓華為進行調用
	 * @param CSPID
	 * @param LSPID
	 * @param correlateID
	 * @param cmdResult
	 * @param resultFileURL
	 * @return
	 */
	public CSPResult resultNotify(String CSPID, String LSPID,
			String correlateID, int cmdResult, String resultFileURL) {
		long start = System.currentTimeMillis();
		// 第一步,獲取url傳遞的XML的內容
		// getC2XML(resultFileURL);
		// log.info("生成的XML的內容是:"+getC2XML(resultFileURL));
		// 第二步,生成返回的結構體
		log.info("進入CSPResult方法");
		log.info("華為提供的CSPID是:" + CSPID);
		log.info("華為提供的LSPID是:" + LSPID);
		log.info("華為提供的correlateID是:" + correlateID);
		log.info("華為提供的cmdResult是:" + cmdResult);
		// 需要將華為的XML下載到我本地來
		// ftp://cms:vtpicms@172.25.39.4/syncdir/remote/CCTV/response/CCTV_SOAP_RESULT_CCTV_SOAP_MSG_201305131454466493664.xml
		log.info("華為提供的XML路徑是:" + resultFileURL);
		
		//下載華為的XML
		String localURL=getC2XML(resultFileURL);
		
		//根據localURL獲取XML內容
		
		//轉化為雅麗的XML
		
		//調用汝雅麗的webservice
		CSPResult result = new CSPResult();
		result.setResult(0);
		result.setErrorDescription("成功");
		log.info("CSPResult方法執行結束");
	    long end = System.currentTimeMillis();
		log.info("**************resultNotify方法所用的時間是:"+(end-start));
		return result;
	}

	/***
	 * 工具包,根據華為提供的返回URL地址,進行解析和FTP資源的遷移
	 * @param resultFileURL
	 * @return localURL
	 */
	private String getC2XML(String resultFileURL) {
		long start = System.currentTimeMillis();
		log.info("進入getC2XML方法");
		FTPTool myFtp = new FTPTool();
		Properties p;
		try {
			p = PropertiesLoader.getProperties("");
			String ftpUrl;
			int ftpPort = PropertiesLoader.getIntegerProperty(p, HUAWEI_FTP_PORT, 21);
			String ftpUsername;
			String ftpPassword;
			String huaweiXmlFilePath;
			String hansciXmlFilePath = PropertiesLoader.getStringProperty(p, HANSCI_REMOTE_XML_FILE_PATH, "E:/CLIP/HUAWEI2HANSCISTORE/xml/");
			String remoteFileName;
			
			String val;
			// 獲得協議
			val = resultFileURL.substring(0, resultFileURL.indexOf("/") - 1);
			System.out.println("Protocal: " + val);
			int startIndex = resultFileURL.indexOf("/") + 2;
			int endIndex = resultFileURL.indexOf(":", startIndex);

			// 獲得用戶名
			val = resultFileURL.substring(startIndex, endIndex);
			ftpUsername = val;
			System.out.println("User: " + val);

			// 獲得密碼
			startIndex = endIndex + 1;
			endIndex = resultFileURL.indexOf("/", startIndex);
			val = resultFileURL.substring(startIndex, endIndex);
			val = resultFileURL.substring(startIndex, resultFileURL.indexOf("@"));
			ftpPassword = val;
			System.out.println("Password: " + val);

			// 默認端口號
			int port = 21;
			System.out.println("Port: " + 21);

			// 獲得url
			val = resultFileURL.substring(resultFileURL.indexOf("@") + 1,
					resultFileURL.length());
			val = val.substring(0, val.indexOf("/"));
			ftpUrl = val;
			System.out.println("Url: " + val);

			// 獲得path
			val = resultFileURL.substring(resultFileURL.indexOf("@") + 1,
					resultFileURL.length());
			val = val.substring(val.indexOf("/"), val.length());
			huaweiXmlFilePath = val;
			System.out.println("Path: " + val);
			
	    	//獲得文件名 
			val = val.substring(val.indexOf("response"),val.length());
	    	val = val.substring(val.indexOf("/")+1,val.length());
	    	remoteFileName = val;
	    	System.out.println("Name:"+remoteFileName);
			
			myFtp.connect(ftpUrl, ftpPort, ftpUsername, ftpPassword);
			System.out.println(myFtp.download(HUAWEI_TITLE_PATH+huaweiXmlFilePath, hansciXmlFilePath+remoteFileName));
			myFtp.disconnect();
			log.info("getC2XML方法執行結束");
		    long end = System.currentTimeMillis();
			log.info("**************功能:根據華為提供的返回URL地址,進行解析和FTP資源的遷移;時間統計:getC2XML方法所用的時間是:"+(end-start));
			return hansciXmlFilePath+remoteFileName;
		}catch (IOException e) {
			System.out.println("連接FTP出錯:" + e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
		
	}
	
	
	private static C2Result parseRemoteURL(String remoteURL) {
		C2Result result = new C2Result();
		File f = new File(remoteURL);
		try {
			String content = FileUtils.readFileToString(f);
			//log.info(content);
			Document retdoc = null;
			retdoc = DocumentHelper.parseText(content);
			Element root = retdoc.getRootElement();
			Element node = root.element("Reply");
			Iterator<Element> its = node.elementIterator("Property");
			while(its.hasNext()){
				Element it = its.next();
				if ("Result".equalsIgnoreCase(it.attributeValue("Name"))) {
//					System.out.println("result");
					result.setResult(it.getTextTrim());
				}
				if ("Description".equalsIgnoreCase(it.attributeValue("Name"))) {
//					System.out.println("Description");
					result.setDescription(it.getTextTrim());
				}
			}
		}catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

//	public static void main(String[] args) {
//		C2Result s =parseRemoteURL("E:/CLIP/HUAWEI2HANSCISTORE/xml/CCTV_SOAP_RESULT_CCTV_SOAP_MSG_201305161550541484784.xml");
//		System.out.println("========"+s.getResult()+"-----"+s.getDescription());
//	}
}


最後更新:2017-04-03 18:51:58

  上一篇:go Git團隊協作使用規範 以及一些常用命令詳解
  下一篇:go redhat6 搭建開發環境