zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    IIS打开本地站点时,无法访问本地json文件的解决办法
    几种流行的前端框架(BootStrap、Layui、Element-UI、Mint UI、Angular、Vue.js、React)
    六大排序算法:插入排序、希尔排序、选择排序、冒泡排序、堆排序、快速排序
    SpringBoot框架开发的优秀的项目「值得收藏学习」
    jmeter接口之json提取器应用
    【设计模式(23)】行为型模式之访问者模式
    【设计模式(22)】行为型模式之模板模式
    【设计模式(21)】行为型模式之策略模式
    HTML回忆笔记,给那些忘了但又没完全忘的人准备的
    vscode创建html文件使用"!+tab"不起作用的解决方法
  • 原文地址:https://www.cnblogs.com/hujia/p/2679763.html
Copyright © 2011-2022 走看看