java对xml文件做增删改查

阅读更多
package com.wss;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

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.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

public class GPS_GNSS_XML_Color {
    //删除节点时传入的参数
    private static String deleteNumber;
    
    //修改节点时传入的参数
    private static String updateNumber;
    
    //读取传入的路径,返回一个document对象
    public static Document loadInit(String filePath){
        Document document = null;
        try{
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            document = builder.parse(new File(filePath));
            document.normalize();
            return document;
        }catch(Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
            return null;
        }
    }

    /**
     * 删除制定的xml
     * @param filePath
     * @return
     */
    public static boolean deleteXML(String filePath){
        deleteNumber = "421f481e-790c-41be-91e3-27d215b73ce2";
        Document document = loadInit(filePath);
        try{
            NodeList nodeList = document.getElementsByTagName("color");
            for(int i=0; i selectXML(String filePath){
         List colorValueList = new ArrayList();
         try{
             //读取传入的路径,返回一个document对象
             Document document = loadInit(filePath);
             //获取叶节点
             NodeList nodeList = document.getElementsByTagName("color");
             //遍历叶节点
             for(int i=0; i 
  

 



    
        7007b384-fab3-4779-9171-229d0664b6b5
        black
        2222
        22222
    
    
        421f481e-790c-41be-91e3-27d215b73ce2
        colorValue
        100
        200
    

 

你可能感兴趣的:(java对xml文件做增删改查)