从ftp上下载文件后,对对应的Element属性进行修改。
package Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import map.util.MediaCodeToContentID;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import download.Download;
import download.DownloadFactory;
public class FileTransferTest {
private static Logger logger = Logger.getLogger(FileTransferTest.class);
public static void main(String args[]){
String cmdFileUrl="ftp://root:123456@127.0.0.1/2.xml";
String xmlFileSavePath="D:/xml";
Download down;
try {
logger.info("Begin download cmdFileUrl:"+cmdFileUrl);
down = DownloadFactory.createDownload(cmdFileUrl, xmlFileSavePath);
down.startDownload();
try{
if(getFileName(cmdFileUrl)!=null && getFileName(cmdFileUrl).length()>0){
DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
DocumentBuilder dombuilder = domfac.newDocumentBuilder();
//将要修改的xml文件导入
InputStream is = new FileInputStream(xmlFileSavePath+"/"+getFileName(cmdFileUrl));
Document doc = dombuilder.parse(is);
NodeList objectVO = doc.getElementsByTagName("Object");
for (int l = 0; l < objectVO.getLength(); l++){
Node objectvo = objectVO.item(l);
NamedNodeMap attributes = objectvo.getAttributes();
String objectType=attributes.getNamedItem("ElementType").getTextContent();
if(objectType!=null&&objectType.equals("Program")){
String idString = attributes.getNamedItem("ID").getTextContent();
MediaCodeToContentID mtoC = new MediaCodeToContentID();
idString = mtoC.convertMediaCode(idString);
((Element)objectvo).setAttribute("ContentID", idString);
attributes.removeNamedItem("ID");
attributes.removeNamedItem("Code");
NodeList programProperties = objectvo.getChildNodes();
String typeValue= "";
for (int s=0; s