读写xml,doc

<?xml version="1.0" encoding="UTF-8"?> 张三 123123 6666666 password

zhangdaihaomima

package com.example.springbootdemo.utils.testXml;
import java.io.FileInputStream;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

/**

  • 解析xml文件参数(增删查改)

*/
@SuppressWarnings(“all”)
public class ReaderXml {

 //解析xml文件
 public static void XmlParse() throws JDOMException, IOException {
	 SAXBuilder builder = new SAXBuilder();
	 InputStream file = new FileInputStream("src/main/java/com/example/springbootdemo/utils/xml/student.xml");
	 Document document = builder.build(file);//获得文档对象
	 Element root = document.getRootElement();//获得根节点
	 List<Element> list = root.getChildren();
	 for(Element e:list) {
		 System.out.println("ID="+e.getAttributeValue("id"));
		 System.out.println("username="+e.getChildText("username"));
		 System.out.println("password="+e.getChildText("password"));
	 }
 }
 
 //增
 public static void addXml() throws JDOMException, FileNotFoundException, IOException {
	 SAXBuilder builder = new SAXBuilder();
	 Document doc = builder.build("src/main/java/com/example/springbootdemo/utils/xml/student.xml");//获得文档对象
	 Element root = doc.getRootElement();//获得根节点
	
	 //添加新元素
	 Element element = new Element("person");
	 	element.setAttribute("id", "3");
	 Element element1 = new Element("username");
	 	element1.setText("zhangdaihao");
	 Element element2 = new Element("password");
	 	element2.setText("mima");
	 	element.addContent(element1);
	 	element.addContent(element2);
	 	root.addContent(element);
	 	doc.setRootElement(root);
  
	 //文件处理
	 XMLOutputter out = new XMLOutputter();
	 out.output(doc, new FileOutputStream("src/main/java/com/example/springbootdemo/utils/xml/student.xml"));
 }
 
 //根据ID值删除一个节点
 public static void deletePerson(int id) throws JDOMException, IOException {
	 SAXBuilder builder = new SAXBuilder();
	 InputStream file = new FileInputStream("src/main/java/com/example/springbootdemo/utils/xml/student.xml");
	 Document doc = builder.build(file);//获得文档对象
	 Element root = doc.getRootElement();//获得根节点
	 List<Element> list = root.getChildren();
	 for(Element e:list) {
		 //获取ID值
		 if(Integer.parseInt(e.getAttributeValue("id"))==id) {
			 root.removeContent(e);
			 break;//??
		 }
	 }
  
	 //文件处理
	 XMLOutputter out = new XMLOutputter();
	 out.output(doc, new FileOutputStream("src/main/java/com/example/springbootdemo/utils/xml/student.xml"));
 }
 
 //根据ID值修改一个节点
 public static void updatePerson(int id) throws JDOMException, IOException {
	 SAXBuilder builder = new SAXBuilder();
	 InputStream file = new FileInputStream("src/main/java/com/example/springbootdemo/utils/xml/student.xml");
	 Document doc = builder.build(file);//获得文档对象
	 Element root = doc.getRootElement();//获得根节点
	 List<Element> list = root.getChildren();
	 for(Element e:list) {
		 //获取ID值
		 if(Integer.parseInt(e.getAttributeValue("id"))==id) {
			 System.out.println("--------------------");
			 e.getChild("username").setText("6666666");
			 e.getChild("password").setText("password");
    
		 }
	 }
  
	 //文件处理
	 XMLOutputter out = new XMLOutputter();
  	out.output(doc, new FileOutputStream("src/main/java/com/example/springbootdemo/utils/xml/student.xml"));
 }

public static void main( String[] args ){
    System.out.println( "Hello World!" );
    try {
		addXml();//增加XML
        //deletePerson(3);//删除XML
        //updatePerson(2);//修改XML
		XmlParse();//解析XML
	} catch (JDOMException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

}

package com.example.springbootdemo.utils.exportdoc;

import java.io.File;
import java.io.FileOutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.rtf.RtfWriter2;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestDoc {

@Resource
private HttpServletResponse httpServletResponse;

@Resource
private HttpServletRequest httpServletRequest;

static{
    System.out.println("我来了");
}

/** 
 * @param args 
 */  
public static void main(String[] args) {  
    exportDoc("E:\\你好.doc");

// ExcelPdfCvcUtil epu = new ExcelPdfCvcUtil();
// epu.exportSchedulesXls();
}

public static void exportDoc(String fileName){  
    try {  
        Document doc = new Document();  
        RtfWriter2.getInstance(doc, new FileOutputStream(fileName));  
        // 打开文档  
        doc.open();  
        //设置页边距,上、下25.4毫米,即为72f,左、右31.8毫米,即为90f  
        doc.setMargins(90f, 90f, 72f, 72f);  

        //设置标题字体样式,粗体、二号、华文中宋  
        Font tfont = DocStyleUtils.setFontStyle("华文中宋", 22f, Font.BOLD);  
        //设置正文内容的字体样式,常规、三号、仿宋_GB2312  
        Font bfont = DocStyleUtils.setFontStyle("仿宋_GB2312", 16f, Font.NORMAL);  

        //构建标题,居中对齐,12f表示单倍行距  
        Paragraph title = DocStyleUtils.setParagraphStyle("测试Itext导出Word文档", tfont, 12f, Paragraph.ALIGN_CENTER);  
        //构建正文内容  
        StringBuffer contentSb = new StringBuffer();  
        contentSb.append("最近项目很忙,这个是项目中使用到的,所以现在总结一下,以便今后可以参考使用,");  
        contentSb.append("2011年4月27日 — 2011年5月20日,对以下技术进行使用,");  
        contentSb.append("Itext、");  
        contentSb.append("Excel、");  
        contentSb.append("Word、");  
        contentSb.append("PPT。");  

        //首行缩进2字符,行间距1.5倍行距  
        Paragraph bodyPar = DocStyleUtils.setParagraphStyle(contentSb.toString(), bfont, 32f, 18f);  
        Paragraph bodyEndPar = DocStyleUtils.setParagraphStyle("截至2011年4月28日,各种技术已经完全实现。", bfont, 32f, 18f);  
        //设置空行  
        Paragraph blankRow = new Paragraph(18f, " ", bfont);  
        Paragraph deptPar = DocStyleUtils.setParagraphStyle("(技术开发部盖章)", bfont, 12f, Paragraph.ALIGN_RIGHT);  
        Paragraph datePar = DocStyleUtils.setParagraphStyle("2011-04-30", bfont, 12f, Paragraph.ALIGN_RIGHT);  

        //向文档中添加内容  
        doc.add(title);  
        doc.add(blankRow);  
        doc.add(bodyPar);  
        doc.add(bodyEndPar);  
        doc.add(blankRow);  
        doc.add(blankRow);  
        doc.add(blankRow);  
        doc.add(deptPar);  
        doc.add(datePar);  

        //最后一定要记住关闭  
        doc.close();  

    } catch (Exception e) {  
        e.printStackTrace();  
    } finally {
		/*if (fileName!=null) {
			boolean flag = deleteFile(fileName); 
		}*/
	} 
} 

public static boolean deleteFile(String sPath) {
	boolean flag = false;
	File file = new File(sPath);
	if (file.isFile() && file.exists()) {
		file.delete();
		flag = true;
	}
	return flag;
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值