DOM4J解析XML

<?xml version="1.0" encoding="UTF-8"?>
<xsi:ADI xmlns:xsi="http://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:[email protected]/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());
//	}
}


你可能感兴趣的:(DOM4J解析XML)