用dom4j建立,修改XML文档,并解决格式化输出和中文

public XMLWriter(OutputStream out) throws UnsupportedEncodingException {



        //System.out.println("In OutputStream");



        this.format = DEFAULT_FORMAT;



        this.writer = createWriter(out, format.getEncoding());



        this.autoFlush = true;



       namespaceStack.push(Namespace.NO_NAMESPACE);



    }



 



    public XMLWriter(OutputStream out, OutputFormat format) throws UnsupportedEncodingException {



        //System.out.println("In OutputStream,OutputFormat");



        this.format = format;



        this.writer = createWriter(out, format.getEncoding());



        this.autoFlush = true;



       namespaceStack.push(Namespace.NO_NAMESPACE);



    }
import java.io.File;

import java.io.FileWriter;

import java.util.Iterator;

import java.util.List;

import java.io.FileOutputStream;

import org.dom4j.Attribute;

import org.dom4j.Document;

import org.dom4j.DocumentHelper;

import org.dom4j.Element;

import org.dom4j.io.OutputFormat;

import org.dom4j.io.SAXReader;

import org.dom4j.io.XMLWriter;



public class firstClass {



	public int ModifyXMLFile(String fileName,String newFileName)

	{

		int returnValue=0;

		try{

			SAXReader reader = new SAXReader();

			Document document = reader.read(new File(fileName));

			List list = document.selectNodes("books/book");

			Iterator iter=list.iterator();

			while(iter.hasNext())

			{

				Element e = (Element)iter.next();

				Attribute attribute = e.attribute("price");

				attribute.setText("55___FCKpd___1quot;);

				Element title = e.element("title");

				title.setText("北京天安门");

				//Element bookElement = (Element)iter.next();

				//System.out.println(bookElement.element("title").getText());

				//Attribute attribute = (Attribute)iter.next();

				//System.out.println(attribute.getValue());

			}

			try{

				XMLWriter writer = new XMLWriter(new FileOutputStream(newFileName));

				writer.write(document);

				writer.close();

				returnValue=1; 

			}catch(Exception ex){

				ex.printStackTrace();

			}

		}

		catch(Exception ex)

		{

			ex.printStackTrace();

		}

		return returnValue;

	}

	public int CreateXMLFile(String filename){

		int returnValue=0;

		

		Document document = DocumentHelper.createDocument();

		Element booksElement = document.addElement("books");

		booksElement.addComment("This is a test for dom4j");

		Element bookElement = booksElement.addElement("book");

		bookElement.addAttribute("price", "11___FCKpd___1quot;);

		Element bookTitle = bookElement.addElement("title");

		bookTitle.setText("星期五");

		

		bookElement = booksElement.addElement("book");

		bookElement.addAttribute("price", "33___FCKpd___1quot;);

		bookTitle = bookElement.addElement("title");

		bookTitle.setText("周三");

		

		try{

			XMLWriter writer = new XMLWriter(new FileOutputStream(filename));

			//输出中文错误new XMLWriter(new FileWriter(new File(filename)));

			writer.write(document);

			writer.close();

			returnValue=1;

		}catch(Exception ex){

			ex.printStackTrace();

		}

		

		return returnValue;

	}

	public static void main(String[] args) {

		// TODO Auto-generated method stub

		firstClass fClass = new firstClass();

		fClass.CreateXMLFile("test.xml");

		fClass.ModifyXMLFile("test.xml","modify.xml");

	}



}

需先导入jaxen-1.1.1.jar和dom4j-1.6.1.jar
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值