java读写xml文件

java源代码

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
public class Operate {
     public static void main(String[] args) throws Exception
     {
         WriteXML(".//conf//12.xml", "searcher.dir", "123");
         System.err.println(ReadXML(".//conf//12.xml","searcher.dir"));
     }
     public static String ReadXML(String path,String name) throws Exception{
             DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
             DocumentBuilder db=dbf.newDocumentBuilder();
             InputStream is=new FileInputStream(path);
             Document doc=db.parse(is);
             Element root=doc.getDocumentElement();
             NodeList datas=root.getChildNodes();
             String str="";
             if(datas!=null){
                 for(int i=0;i<datas.getLength();i++){
                     Node data=datas.item(i);
                     if(data.getNodeType()==Node.ELEMENT_NODE){
                         for(Node node=data.getFirstChild();node!=null;node=node.getNextSibling()){
                             if(node.getNodeType()==Node.ELEMENT_NODE){
                                 if(node.getNodeName().equals("name")&&node.getTextContent().equals(name)){
                                     NodeList nl=node.getParentNode().getChildNodes();
                                     for (int k = 0; k <nl.getLength(); k++) { 
                                         if (nl.item(k).getNodeName().contains("value")) {
                                            str=nl.item(k).getTextContent();
                                        }
                                         } 
                                    }
                                     }
                                 }
                             }
                         }
                     }
             return str;
         }
    
     public static void WriteXML(String path,String name,String value){
         DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();
         try {
             DocumentBuilder dombuilder=domfac.newDocumentBuilder();
             InputStream is=new FileInputStream(path);
             Document doc=dombuilder.parse(is);
             Element root=doc.getDocumentElement();
             NodeList datas=root.getChildNodes();
             if(datas!=null){
                 for(int i=0;i<datas.getLength();i++){
                     Node data=datas.item(i);
                     if(data.getNodeType()==Node.ELEMENT_NODE){
                         for(Node node=data.getFirstChild();node!=null;node=node.getNextSibling()){         
                             if(node.getNodeName().equals("name")&&node.getTextContent().equals(name)){
                                 NodeList nl=node.getParentNode().getChildNodes();
                                 for (int k = 0; k <nl.getLength(); k++) { 
                                     if (nl.item(k).getNodeName().contains("value")) {
                                        nl.item(k).setTextContent(value);
                                    }
                                     } 
                                }
                             }
                         }
                     }
                 }
             doc2XmlFile(doc, path);    
             System.err.println("success");
         } catch (ParserConfigurationException e) {
                 e.printStackTrace();
                 } catch (FileNotFoundException e) {
                     e.printStackTrace();
                     } catch (SAXException e) {
                         e.printStackTrace();
                         } catch (IOException e) {
                             e.printStackTrace();
                             }
         }
     public static boolean doc2XmlFile(Document document, String filename) {
            boolean flag = true;
            try {
               
                TransformerFactory tFactory = TransformerFactory.newInstance();
                Transformer transformer = tFactory.newTransformer();
                DOMSource source = new DOMSource(document);
                StreamResult result = new StreamResult(new File(filename));
                transformer.transform(source, result);
            } catch (Exception ex) {
                flag = false;
                ex.printStackTrace();
            }
            return flag;
        }
 }

xml结构

<?xml version="1.0" encoding="UTF-8" standalone="no"?><?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
    <name>http.agent.name</name>
    <value>my nutch agent</value>
</property>
<property>
    <name>http.agent.version</name>
    <value>1.2</value>
</property>
<property>
    <name>searcher.dir</name>
    <value>123</value>
    <description>index dir</description>
</property>
</configuration>
原文地址:https://www.cnblogs.com/dgy5554/p/3973411.html