java xml

package com.lifz.server;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
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.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;

public class XmlServer {
	public static void main(String[] args) {
		String filePath = "D:\\Users\\lifazhu\\Desktop\\xmltest\\Teams.xml";
		readXml(filePath);
	}
	private static void readXml(String filePath) {
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbf.newDocumentBuilder();
			Document doc = dBuilder.parse(filePath);
			doc.normalize();
			Element root = doc.getDocumentElement();
			
			addNode(doc);	// 添加节点
			if(doc != null) printNode(root); // 打印节点
			createXml(doc) ; // 创建xml
			deleteNode(doc);// 删除节点
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private static void createXml(Document doc) {
		try {
			TransformerFactory trFactory = TransformerFactory.newInstance();
			Transformer  tr = trFactory.newTransformer();
			doc.normalize();
			DOMSource source = new DOMSource(doc);
			String outFile = "D:\\Users\\lifazhu\\Desktop\\xmltest\\OutTeams.xml";
			StreamResult result = new StreamResult(new File(outFile));
			tr.setOutputProperty(OutputKeys.ENCODING, "utf-8");  
			tr.setOutputProperty(OutputKeys.INDENT,"yes");
			tr.transform(source, result);
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (TransformerFactoryConfigurationError e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		}

	}

	private static void printNode(Element root) {
		NodeList children = root.getChildNodes();
		NamedNodeMap attr = root.getAttributes();
		int r = children.getLength();
		if (attr != null) {
			System.out.print("<" + root.getNodeName());
			for (int i = 0; i < attr.getLength(); i++) {
				System.out.println(" " + attr.item(i).getNodeName() + "=" + attr.item(i).getNodeValue());
			}
			System.out.print(">");
		}else {
			System.out.println("<"+root.getNodeName()+">");
		}

		if(root.hasChildNodes()){
			for (int i = 0; i < r ;i++) {
				if(children.item(i).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE){
					printNode((Element) children.item(i));
				}else if(children.item(i).getNodeType() == org.w3c.dom.Node.TEXT_NODE){
					System.out.println(children.item(i).getNodeValue());
				}
			}
			System.out.println("</"+root.getNodeName() + ">");
		}
		
	}
	
	private static void addNode(Document doc) {
		Element root = doc.getDocumentElement();
		
		Element newChild = doc.createElement("Team");
		
		Element e_TeamName = doc.createElement("TeamName"); 
		Text t_TeamName = doc.createTextNode("AC Milan2");
		e_TeamName.appendChild(t_TeamName);
		
		Element e_Countary = doc.createElement("Countary"); 
		Text t_Countary = doc.createTextNode("Italy");
		e_Countary.appendChild(t_Countary);
		
		Element e_Member = doc.createElement("Member"); 
		Text t_Member = doc.createTextNode("Pippo");
		e_Member.appendChild(t_Member);
		
		Attr attr_Age = doc.createAttribute("Age");
		attr_Age.setNodeValue("32");
		Attr attr_Sex = doc.createAttribute("Sex");
		attr_Sex.setNodeValue("female");
		
		e_Member.setAttributeNode(attr_Age);
		e_Member.setAttributeNode(attr_Sex);
		
		newChild.appendChild(e_TeamName);
		newChild.appendChild(e_Countary);
		newChild.appendChild(e_Member);
		
		root.appendChild(newChild);	// 添加节点
	}
	
	private static void deleteNode(Document doc) {
		Element root = doc.getDocumentElement();
		Node oldChild = root.getLastChild();
		root.removeChild(oldChild);
		System.out.println("开始打印删除节点后的Node");
		printNode(root);
	}
}

你可能感兴趣的:(java,xml)