W3C 解析XML

W3C解析  负责  XML


package com.itheima.xml;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Iterator;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

import com.itheima.xml.bean.Plant;

public class TextW3CParseXML {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String fileName="F:/plant.xml";  //已经存在的 xml文件
		TextW3CParseXML t=new TextW3CParseXML();
		try {
			Vector v=t.readXMLFile(fileName);
			Iterator it=v.iterator();
			while(it.hasNext()){
				Plant s=(Plant)it.next();
				System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getDesc());
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("读取XML文件出错:"+e.getMessage());
		}
		String outfile="F:/plant1.xml";  //复制到这个文件中
		t.writeXMLFile(outfile);
	}
	
	Vector plant_Vector;
	
	private Vector readXMLFile(String fileName) throws Exception{
		//获取dom解析器的工厂实例 
		DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
		
		DocumentBuilder build=dbf.newDocumentBuilder();
		
		Document doc=build.parse(fileName);
		
		Element root=doc.getDocumentElement();
		NodeList plantList=root.getElementsByTagName("plant");
		plant_Vector=new Vector();
		System.out.println(plantList.getLength());
		
		for (int i = 0; i < plantList.getLength(); i++) {
			Element element=(Element)plantList.item(i);
			
			Plant plant=new Plant();
			plant.setId(element.getAttribute("id"));
			
			NodeList names=element.getElementsByTagName("name");
			Element e=(Element)names.item(0);
			Node t=e.getFirstChild();
			plant.setName(t.getNodeValue());
			
			NodeList desc=element.getElementsByTagName("description");
			e=(Element)desc.item(0);
			t=e.getFirstChild();
			plant.setDesc(t.getNodeValue());
			
			plant_Vector.add(plant);
		}
		return plant_Vector;
		
	}
	
	//将文档写入到XML文件中
	public static void callWriteXmlFile(Document doc,Writer w,String encoding){
		try {
			Source source=new DOMSource(doc);
			Result result=new StreamResult(w); //创建结果对象
			Transformer transformer=TransformerFactory.newInstance().newTransformer(); //创建转换器
			transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
			transformer.transform(source, result); //对文档进行转换
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	//将 XML文件复制到指定文件
	private void writeXMLFile(String fileName){
		DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
		DocumentBuilder builder=null;
		try {
			builder=dbf.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Document doc=builder.newDocument();
		Element root=doc.createElement("plants");
		doc.appendChild(root);
		for (int i = 0; i < plant_Vector.size(); i++) {
			Plant plant=(Plant)plant_Vector.get(i);
			Element element=doc.createElement("plant");
			element.setAttribute("id", plant.getId());
			root.appendChild(element);
			
			Element name=doc.createElement("name");
			element.appendChild(name);
			Text tname=doc.createTextNode(plant.getName());
			name.appendChild(tname);
			
			Element desc=doc.createElement("description");
			element.appendChild(desc);
			Text tdesc=doc.createTextNode(plant.getDesc());
			desc.appendChild(tdesc);
			
		}
		try {
			FileOutputStream fos=new FileOutputStream(fileName);  //创建文件输出流
			
			OutputStreamWriter outWriter=new OutputStreamWriter(fos);
			callWriteXmlFile(doc, outWriter, "gb2312");
			outWriter.close();
			fos.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		
	}
	

}

你可能感兴趣的:(XML,W3C)