• java基础 创建xml文件


    <span style="font-size:18px;">package com.demo;
    
    import java.io.File;
    import java.io.StringWriter;
    import java.security.KeyStore.Builder;
    
    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;
    
    public class createxml {
    
    	public static void main(String[] args) {
    
    		try {
    			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    			DocumentBuilder builder = factory.newDocumentBuilder();
    			Document document = builder.newDocument();
    			
    			//创建根节点
    			Element root = document.createElement("languages");
    			root.setAttribute("cat", "it");
    			
    			Element lan1 = document.createElement("lan");
    			lan1.setAttribute("id", "1");
    			Element name1 = document.createElement("name");
    			name1.setTextContent("java");
    			Element ide1 = document.createElement("ide");
    			ide1.setTextContent("eclipse");
    			//以lan1为父节点,加入name1和ide1子节点
    			lan1.appendChild(name1);
    			lan1.appendChild(ide1);
    			
    			Element lan2 = document.createElement("lan");
    			lan2.setAttribute("id", "2");
    			Element name2 = document.createElement("name");
    			name2.setTextContent("Swift");
    			Element ide2 = document.createElement("ide");
    			ide2.setTextContent("Xcode");
    			lan2.appendChild(name2);
    			lan2.appendChild(ide2);
    			
    			Element lan3 = document.createElement("lan");
    			lan3.setAttribute("id", "3");
    			Element name3 = document.createElement("name");
    			name1.setTextContent("C#");
    			Element ide3 = document.createElement("ide");
    			ide3.setTextContent("Visual Studio");
    			lan3.appendChild(name3);
    			lan3.appendChild(ide3);
    			
    			//以root为根节点加入三个lan子节点,再将root加入document.
    			root.appendChild(lan1);
    			root.appendChild(lan2);
    			root.appendChild(lan3);
    			document.appendChild(root);
    			
    			//-----------------------------------------------
    			//以上为创建xml文件内容,下面需要将该内容生成xml文件,或者以其他方式保存
    			
    			TransformerFactory transformerFactory = TransformerFactory.newInstance();
    			Transformer transformer = transformerFactory.newTransformer();
    			StringWriter writer = new StringWriter();
    			transformer.transform(new DOMSource(document), new StreamResult(writer));
    			System.out.println(writer.toString());
    			
                            //将创建的xml保存至项目路径下,运行后刷新项目即可看到
    			transformer.transform(new DOMSource(document), new StreamResult(new File("languages.xml")));
    			
    		} catch (ParserConfigurationException e) {
    			e.printStackTrace();
    		} catch (TransformerConfigurationException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (TransformerException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    }
    </span>


    控制台效果如下:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?><languages cat="it"><lan id="1"><name>C#</name><ide>eclipse</ide></lan><lan id="2"><name>Swift</name><ide>Xcode</ide></lan><lan id="3"><name/><ide>Visual Studio</ide></lan></languages>

    提示:当然生成的xml文件是没有格式化的,你也可以打开生成的languages.xml文件使用快捷点Ctrl + i格式化





  • 相关阅读:
    使用Pandas groupby连接来自多行的字符串
    Pandas数据分析介绍
    SQL Server 32位数据源与64位数据源区别
    SQL Server install
    windows 远程提示CredSSP
    linux 终端下以图形界面打开当前文件夹
    Linux g++ include link
    undefined reference to symbol 'pthread_create@@GLIBC_2.2.5'
    Linux下的库操作工具-nm、ar、ldd、ldconfig和ld.so
    git update
  • 原文地址:https://www.cnblogs.com/laohuihui/p/5308749.html
Copyright © 2020-2023  润新知