现在的位置: 首页 > 综合 > 正文

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

2013年10月11日 ⁄ 综合 ⁄ 共 2683字 ⁄ 字号 评论关闭
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

抱歉!评论已关闭.