package test;import java.io.File;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;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.NodeList;/*** * @author echo lovely* * dom模式下的* xml文件的解析,讀取,增刪改* * 優缺點:* 1. 消耗內存大,把整個xml讀到內存,適合數據少的讀取* 2. 靈活,可對整個xml文檔進行操作,操作父子,兄弟,第一個,最后一個結點* * 排版:* DocumentBuliderFactory BuilderFactory Document* * 印刷:* TransformerFactory Transformer transfome* */publicclassDocumentXMLNode{publicstaticvoidmain(String[] args)throws Exception {// createNew();// parserXML();// DOMAddXML();// DOMUpdateXML();DOMDeleteXML();}// one by onestaticvoidcreateXML()throws Exception {// 創建一個排版工廠DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();// 創建一個排版機器DocumentBuilder db = factory.newDocumentBuilder();// 準備好空白文檔,準備排版Document doc = db.newDocument();// 創建根節點Element root = doc.createElement("Student");// 把根結點掛載到文檔中doc.appendChild(root);for(int i =0; i <3; i ++){// 創建stu結點Element stu = doc.createElement("stu"+(i +1));// 把元素結點添加到stu結點下root.appendChild(stu);// 設置element的屬性stu.setAttribute("id",(i +1)+"");// 創建學生的姓名結點Element name = doc.createElement("name");name.setTextContent("小明"+ i);stu.appendChild(name);// 學生的年齡結點Element age = doc.createElement("age");age.setTextContent(""+(18+ i));stu.appendChild(age);}// 印刷// 創建印刷工場TransformerFactory tff = TransformerFactory.newInstance();// 印刷機器Transformer tf = tff.newTransformer();// 開始印刷// 創建xml數據源DOMSource source =newDOMSource(doc);// 確定輸出位置StreamResult res =newStreamResult("stu.xml");tf.transform(source, res);}// 排版 or 解析static Document DOMComposing(String path)throws Exception {// 排版工場DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();// 排版機器DocumentBuilder db = dbf.newDocumentBuilder();Document doc = null;// 空白文檔,準備排版if(path ==""|| path == null)doc = db.newDocument();else doc = db.parse(path);// 解析xmlreturn doc;}/*** @param doc 排版文檔* @param xml文件輸出路徑* @throws Exception */staticvoidDOMPrinting(Document doc, String outputPath)throws Exception {// 排版工場TransformerFactory tff = TransformerFactory.newInstance();// 排版機器Transformer tf = tff.newTransformer();// 數據源DOMSource xmlSource =newDOMSource(doc);// 輸出地址,開始排版 file:/// 中文路徑亂碼StreamResult res =newStreamResult(newFile(outputPath));tf.transform(xmlSource, res);}// hide difficultiesstaticvoidcreateNew(){try{Document doc =DOMComposing("");Element root = doc.createElement("person");doc.appendChild(root);for(int i =0; i <3; i ++){Element teacher = doc.createElement("teacher");teacher.setAttribute("id",""+(i +1));root.appendChild(teacher);Element name = doc.createElement("name");// 設置name結點的文本內容name.setTextContent("cici"+(i +1));Element age = doc.createElement("age");age.setTextContent((24+ i)+"");// 把姓名和年齡添加到teacher結點teacher.appendChild(name);teacher.appendChild(age);}DOMPrinting(doc,"person.xml");}catch(Exception e){e.printStackTrace();}}staticvoidparserXML()throws Exception {// 排版解析DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();DocumentBuilder db = dbf.newDocumentBuilder();// 解析xmlDocument doc = db.parse(newFile("stu.xml"));// Element root = (Element) doc.getFirstChild();Element root = doc.getDocumentElement();NodeList nodes = root.getChildNodes();System.out.println(nodes.item(0));for(int i =0; i < nodes.getLength(); i++){Element stu =(Element) nodes.item(i);String text = nodes.item(i).getFirstChild().getTextContent();System.out.println(stu.getAttribute("id")+"\t"+ text);}}}
element獲取結點的其它方式
Element root = doc.getDocumentElement();root.getFirstChild();//獲得當前節點第一個子節點root.getLastChild();//獲得當前節點最后一個子節點root.getNextSibling();//獲得當前節點下一個兄弟節點root.getPreviousSibling();//獲得當前節點上一個兄弟節點root.getParentNode();//獲得當前節點父節點
2.2 使用dom解析xml文件
對books.xml解析
staticvoiddomParse(){Document doc =docComposing("books.xml");Element root =(Element) doc.getFirstChild();NodeList list = root.getChildNodes();for(int i =0; i < list.getLength(); i++){Element book =(Element) list.item(i);System.out.println(book.getAttribute("id")+"\t"+ book.getTextContent());}}/**
1001 java
1002 c#
*/