java写入xml

<<persons.xml>> 
<?xml version="1.0" encoding="UTF-8"?> 
<persons> 
    <person> 
        <name>andy</name> 
        <age>45</age> 
        <tel>13632940025</tel> 
        <sex>m</sex> 
    </person> 
</persons> 
<<WriteXmlTest.xml>> 
package test.xml; 
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.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.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.NodeList; 
import org.w3c.dom.Text; 
import org.xml.sax.SAXException; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.Statement; 
import java.util.ArrayList; 
public class WriteXmlTest { 
    public static void main(String[] args) { 
        try { 
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
            DocumentBuilder db = dbf.newDocumentBuilder(); 
            Document dc = db.parse("persons.xml"); 
            fang(dc); 
        } catch (ParserConfigurationException e) { 
            e.printStackTrace(); 
        } catch (SAXException e) { 
            e.printStackTrace(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
    } 
 
    public static void fang(Document doc) { 
        Element eleroots = doc.getDocumentElement(); 
 
        Element eleroot = doc.createElement("person");// 创建gen元素  
        eleroots.appendChild(eleroot); 
        Element elename = doc.createElement("name"); 
 
        Text text1 = doc.createTextNode("andy"); 
        elename.appendChild(text1); 
        eleroot.appendChild(elename); 
 
        Element eleage = doc.createElement("age"); 
        Text text2 = doc.createTextNode("45"); 
        eleage.appendChild(text2); 
        eleroot.appendChild(eleage); 
 
        Element eletel = doc.createElement("tel"); 
        Text text3 = doc.createTextNode("13632940025"); 
        eletel.appendChild(text3); 
        eleroot.appendChild(eletel); 
 
        Element elesex = doc.createElement("sex"); 
        Text text4 = doc.createTextNode("m"); 
        elesex.appendChild(text4); 
        eleroot.appendChild(elesex); 
 
        TransformerFactory tff = TransformerFactory.newInstance(); 
        Transformer tf = null; 
        try { 
            tf = tff.newTransformer(); 
        } catch (TransformerConfigurationException e) { 
            e.printStackTrace(); 
        } 
        DOMSource ds = new DOMSource(doc); 
        StreamResult sr = new StreamResult(new File("persons.xml")); 
        try { 
            tf.transform(ds, sr); 
        } catch (TransformerException e) { 
            e.printStackTrace(); 
        } 
    } 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值