dom 解析 XML

   

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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.Attr;
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 org.xml.sax.SAXException;

public class OperatorXmlTest {
	
    public static void write(){
    	DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    	try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			InputStream inputStream = OperatorXmlTest.class.getResourceAsStream("students.xml");
			Document document = builder.parse(inputStream);
			//root <students>
		    Element root = document.getDocumentElement();
		    if(root != null){
		    	root.setAttribute("id", "学生集体");
		     //得到<student>
		    NodeList studentList = root.getChildNodes();
		    if(studentList != null){
		    	for(int i = 0;i<studentList.getLength();i++){
		    		Node student = studentList.item(i);
		    		if(student != null && student.getNodeType() == Node.ELEMENT_NODE){
		    			String studentName = student.getAttributes().getNamedItem("id").getNodeValue();
		    		   if("001".equals(studentName)){
		    			  Element newElement = document.createElement("sex");
		    			  newElement.setNodeValue("男");
		    			  student.appendChild(newElement);
		    		   }
		    		   if("002".equals(studentName)){
		    			  Attr attr = document.createAttribute("id");
		    			  attr.setValue("004");
		    			  student.getAttributes().removeNamedItem("id");
		    			  student.getAttributes().setNamedItem(attr);
		    		   }
		    		}
		    	}
		    }
		    Element stument = document.createElement("student");
		    stument.setAttribute("id", "005");
		    Element namement = document.createElement("name");
		    Text text = document.createTextNode("新增");
			namement.appendChild(text);
		    stument.appendChild(namement);
		    root.appendChild(stument);
		    
		    TransformerFactory transformerFactory = TransformerFactory.newInstance();
		    Transformer transformer = transformerFactory.newTransformer();
		    DOMSource domSource = new DOMSource(document);
		    File file = new File("src/com/aoxun/test/student-nodify.xml");
			if(file.exists()){
				file.delete();
			}
			file.createNewFile();
		    OutputStream outputStream = new FileOutputStream(file);
		    StreamResult result = new StreamResult(outputStream);
		    transformer.transform(domSource, result);
		   }
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		}
    }

    public static void main(String[] args) {
		write();
	}
}

  

  写入参考于:http://www.cnblogs.com/ishang/archive/2012/08/20/2647250.html

posted @ 2012-09-11 10:23  胖hu  阅读(172)  评论(0编辑  收藏  举报