java XML导出


//**********************主要代码**************************

package cn.com.ctdos.info.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;


import org.w3c.dom.CDATASection;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


import cn.com.ctdos.common.util.Parameter;
import cn.com.ctdos.info.model.InfoCommonModel;


/**
 * XmlDom操作
 * @Description: DomFactory.java
 * @author zhanghui
 * @createDate 2017-1-10 下午5:38:53
 */
public class DomFactory {

/**
* 获取xml文件生成Document对象
* @Description: DomFactory.java
* @author zhanghui
* @createDate 2017-1-10 下午5:51:01
* @return
*/
public Document createDocument(){
Document document = null;
try {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
String prjPath = Parameter.absoluteProPath;
FileInputStream  xmlInputStream = new FileInputStream(prjPath+"xml\\template\\模板.xml");//xml模板路径
DocumentBuilder builder = builderFactory.newDocumentBuilder();
document = builder.parse(xmlInputStream);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return document;
}

/**
* 保存xml文件
* @Description: DomFactory.java
* @author zhanghui
* @createDate 2017-1-10 下午5:50:25
* @param document
* @param path 保存文件路径
*/
public void saveXml(Document document,String path){
TransformerFactory transformerFactory = TransformerFactory.newInstance();
try {
Transformer transformer = transformerFactory.newTransformer();
DOMSource domSource = new DOMSource(document);
StreamResult streamResult = new StreamResult(new FileOutputStream(path));
transformer.transform(domSource, streamResult);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}

/**
* 修改节点内容
* @Description: DomFactory.java
* @author zhanghui
* @createDate 2017-1-10 下午5:53:46
*/
public void editText(Document document,String path,InfoCommonModel comm){
Element root = document.getDocumentElement();
NodeList stuNodeList = root.getChildNodes();  
   for(int i = 0;i < stuNodeList.getLength();i ++){  
       Node stuInforNode = stuNodeList.item(i);  
       if(stuInforNode.getNodeType() == Node.ELEMENT_NODE){  
           NodeList stuNode = stuInforNode.getChildNodes();  
           for(int j = 0;j < stuNode.getLength();j ++){  
               Node stuNode2 = stuNode.item(j);
               if(stuNode2.getNodeName().equals("PROPERTIES")){
                NodeList stuNodeList2=stuNode2.getChildNodes();
                for (int k = 0;k < stuNodeList2.getLength();k ++) {
                Node stuNode3 = stuNodeList2.item(k);
                if(!stuNode3.getNodeName().equals("#text")){
                editNode(document,stuNode3,comm);
                }
}
               }
           }  
       }  
   }  
saveXml(document,path);
}

/**
* 修改节点内容
* @Description: DomFactory.java
* @author zhanghui
* @createDate 2017-1-11 上午11:50:56
* @param node
* @param com
*/
private void editNode(Document document,Node node,InfoCommonModel com){
SimpleDateFormat sdf =   new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// if(node.getNodeName().equals("DOCID"))//
// node.setTextContent("70427");
// else if(node.getNodeName().equals("DOCVERSION"))//版本号
// node.setTextContent("0");
// else if(node.getNodeName().equals("DOCTYPE"))//
// node.setTextContent("20");
// else
if(node.getNodeName().equals("DOCPUBTIME"))//发布日期
node.setTextContent(sdf.format(com.getFinishDate()));
else if(node.getNodeName().equals("CRUSER"))//发布人
setCDATA(document,node,com.getFinishUserName());
else if(node.getNodeName().equals("CRTIME"))//创建时间
node.setTextContent(sdf.format(com.getCreateDate()));
// else if(node.getNodeName().equals("DOCFLAG"))//
// node.setTextContent("0");
// else if(node.getNodeName().equals("ATTRIBUTE"))//属性
// setCDATA(document,node," ");
// else if(node.getNodeName().equals("ATTACHPIC"))//附件照片
// node.setTextContent("0");
// else if(node.getNodeName().equals("DOCLINK"))//链接
// {
// if(isNull(com.getInfoLink())){
// setCDATA(document,node,com.getInfoLink().getUrl());
// }else{
// setCDATA(document,node," ");
// }
// }
// else if(node.getNodeName().equals("DOCFILENAME"))//附件名称
// setCDATA(document,node," ");
else if(node.getNodeName().equals("DOCCHANNEL"))//
node.setTextContent("8202");
else if(node.getNodeName().equals("DOCTITLE"))//标题
setCDATA(document,node,com.getTitle());
else if(node.getNodeName().equals("DOCPEOPLE"))//人
setCDATA(document,node,com.getTitle());
// else if(node.getNodeName().equals("DOCSOURCENAME"))//源名称
// setCDATA(document,node," ");
// else if(node.getNodeName().equals("DOCSTATUS"))//状态
// node.setTextContent("10");
else if(node.getNodeName().equals("DOCCONTENT"))//文本内容
{
if(isNull(com.getInfoArticle())){
String val=com.getInfoArticle().getContent();
val=val.replaceAll("</?[^>]+>", "");//剔出<html>的标签
val=val.replaceAll("(?m)^\\s*$(\\n|\\r\\n)", "");//剔出空行
setCDATA(document,node,val); 
}else{
setCDATA(document,node," ");
}
}
else if(node.getNodeName().equals("DOCHTMLCON"))//含html内容
{
if(isNull(com.getInfoArticle())){
String val=com.getInfoArticle().getContent();
if(val.indexOf("<?xml:namespace prefix = \"o\" />")!=-1){
val=val.replace("<?xml:namespace prefix = \"o\" />", "");
}
setCDATA(document,node,val);
}else{
setCDATA(document,node," ");
}
}
// else if(node.getNodeName().equals("DOCWORDSCOUNT"))//单词个数
// node.setTextContent("308");
// else if(node.getNodeName().equals("DOCABSTRACT"))//摘要
// setCDATA(document,node," ");
else if(node.getNodeName().equals("DOCKEYWORDS"))//关键字
{
if(isNull(com.getInfoArticle())){
setCDATA(document,node,com.getInfoArticle().getKeyWord());
}else{
setCDATA(document,node," ");
}
}
// else if(node.getNodeName().equals("DOCAUTHOR"))//作者
// {
// if(isNull(com.getInfoArticle())){
// setCDATA(document,node,com.getInfoArticle().getAuthor());
// }else{
// setCDATA(document,node," ");
// }
// }
// else if(node.getNodeName().equals("DOCRELTIME"))//实时
// node.setTextContent(sdf.format(com.getFinishDate()));
// else if(node.getNodeName().equals("TITLECOLOR"))//标题颜色
// setCDATA(document,node," ");
// else if(node.getNodeName().equals("SUBDOCTITLE"))//子文档的标题
// setCDATA(document,node," ");
// else if(node.getNodeName().equals("CHNLNAME"))//频道名称
// {
// if(isNull(com.getInfoArticle())){
// setCDATA(document,node,com.getInfoArticle().getOriginal());
// }else{
// setCDATA(document,node," ");
// }
// }


/**
* 添加CDATA(解析忽略)节点
* @Description: DomFactory.java
* @author zhanghui
* @createDate 2017-1-11 下午4:04:14
* @param document
* @param node
* @param val 添加文本
*/
private void setCDATA(Document document,Node node,String val){
CDATASection cdata = document.createCDATASection(val);
node.appendChild(cdata);
}

/**
* 是否为空
* @Description: DomFactory.java
* @author zhanghui
* @createDate 2017-1-11 上午11:51:13
* @param obj
* @return
*/
private boolean isNull(Object obj){
if(obj!=null){
return true;
}else{
return false;
}
}


}



//*****************调用*********************

DomFactory dom=new DomFactory();
Document document=dom.createDocument();
String path = Parameter.absoluteProPath+"xml\\export\\"+infoCommonModel.getInfoId()+".xml";
dom.editText(document, path, infoCommonModel);


//********************xml模板***********************

<?xml version="1.0" encoding="UTF-8"?>


<WCMDOCUMENTS>
  <WCMDOCUMENT Version="6.0">
    <PROPERTIES>
      <DOCID>70427</DOCID>
      <DOCVERSION>0</DOCVERSION>
      <DOCTYPE>20</DOCTYPE>
      <DOCPUBTIME>2017-01-06 10:28:28</DOCPUBTIME>
      <CRUSER><![CDATA[]]></CRUSER>
      <CRTIME>2017-01-04 10:40:33</CRTIME>
      <DOCFLAG>0</DOCFLAG>
      <ATTRIBUTE/>
      <ATTACHPIC>0</ATTACHPIC>
      <DOCLINK><![CDATA[]]></DOCLINK>
      <DOCFILENAME><![CDATA[]]></DOCFILENAME>
      <DOCCHANNEL></DOCCHANNEL>
      <DOCTITLE><![CDATA[]]></DOCTITLE>
      <DOCPEOPLE><![CDATA[]]></DOCPEOPLE>
      <DOCSOURCENAME><![CDATA[]]></DOCSOURCENAME>
      <DOCSTATUS>10</DOCSTATUS>
      <DOCCONTENT><![CDATA[]]></DOCCONTENT>
      <DOCHTMLCON><![CDATA[]]></DOCHTMLCON>
      <DOCWORDSCOUNT>308</DOCWORDSCOUNT>
      <DOCABSTRACT><![CDATA[]]></DOCABSTRACT>
      <DOCKEYWORDS></DOCKEYWORDS>
      <DOCAUTHOR><![CDATA[]]></DOCAUTHOR>
      <DOCRELTIME>2017-01-04 10:39:00</DOCRELTIME>
      <TITLECOLOR><![CDATA[]]></TITLECOLOR>
      <SUBDOCTITLE><![CDATA[]]></SUBDOCTITLE>
      <CHNLNAME></CHNLNAME>
    </PROPERTIES>
  </WCMDOCUMENT>
</WCMDOCUMENTS>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值