博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java_XML操作
阅读量:5142 次
发布时间:2019-06-13

本文共 4339 字,大约阅读时间需要 14 分钟。

 

xml的写:

code:

 

1 import java.io.File; 2 import java.io.StringWriter; 3  4 import javax.xml.parsers.DocumentBuilder; 5 import javax.xml.parsers.DocumentBuilderFactory; 6 import javax.xml.parsers.ParserConfigurationException; 7 import javax.xml.transform.Transformer; 8 import javax.xml.transform.TransformerConfigurationException; 9 import javax.xml.transform.TransformerException;10 import javax.xml.transform.TransformerFactory;11 import javax.xml.transform.dom.DOMSource;12 import javax.xml.transform.stream.StreamResult;13 14 import org.w3c.dom.Document;15 import org.w3c.dom.Element;16 17 public class WriteXML {18 19     public static void main(String[] args) {20         21         try {22             23             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();24             DocumentBuilder builder = factory.newDocumentBuilder();25             Document document = builder.newDocument();26             Element root =  document.createElement("Languages");27             root.setAttribute("cat", "it");28             29             Element lan1 = document.createElement("lan");30             lan1.setAttribute("id", "1");31             Element name1 = document.createElement("name");32             Element ide1 = document.createElement("ide");33             name1.setTextContent("Java");34             ide1.setTextContent("eclipse");35             lan1.appendChild(name1);36             lan1.appendChild(ide1);37             38             Element lan2 = document.createElement("lan");39             lan1.setAttribute("id", "2");40             Element name2 = document.createElement("name");41             Element ide2 = document.createElement("ide");42             name2.setTextContent("Swift");43             ide2.setTextContent("Xcode");44             lan2.appendChild(name2);45             lan2.appendChild(ide2);46             47             Element lan3 = document.createElement("lan");48             lan3.setAttribute("id", "3");49             Element name3 = document.createElement("name");50             Element ide3 = document.createElement("ide");51             name3.setTextContent("C#");52             ide3.setTextContent("Visual Studio");53             lan3.appendChild(name3);54             lan3.appendChild(ide3);55             56             root.appendChild(lan1);57             root.appendChild(lan2);58             root.appendChild(lan3);59             document.appendChild(root);60             61             //-----------------------62             63             TransformerFactory transformerFactory = TransformerFactory.newInstance();64             Transformer tansformer = transformerFactory.newTransformer();65             StringWriter writer = new StringWriter();66             tansformer.transform(new DOMSource(document), new StreamResult(writer));67             System.out.println(writer.toString());//输出到控制台68         69             70             tansformer.transform(new DOMSource(document), new StreamResult(new File("newxml.xml")));//输出到xml文本71         } catch (ParserConfigurationException e) {72             73             e.printStackTrace();74         } catch (TransformerConfigurationException e) {75             e.printStackTrace();76         } catch (TransformerException e) {77         78             e.printStackTrace();79         }80     }81 82 }

xml的读:

code:

1 import java.io.File; 2 import java.io.IOException; 3  4 import javax.xml.parsers.DocumentBuilder; 5 import javax.xml.parsers.DocumentBuilderFactory; 6 import javax.xml.parsers.ParserConfigurationException; 7  8 import org.w3c.dom.Document; 9 import org.w3c.dom.Element;10 import org.w3c.dom.Node;11 import org.w3c.dom.NodeList;12 import org.xml.sax.SAXException;13 14 public class TestReadXML {15 16     public static void main(String[] args) {17         //DOM18         try {19             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();20             DocumentBuilder builder =  factory.newDocumentBuilder();21             Document document =  builder.parse(new File("languages.xml"));22             Element root = document.getDocumentElement();//获取根元素23             System.out.println("cat="+root.getAttribute("cat"));24             NodeList list =    root.getElementsByTagName("lan");//获取子元素lan25             for(int i=0;i

 

转载于:https://www.cnblogs.com/UniqueColor/p/5725836.html

你可能感兴趣的文章
Jmeter + Grafana搭建实时监控可视化
查看>>
uCGUI字符串显示过程分析和uCGUI字库的组建
查看>>
h5唤起app
查看>>
SQL Server 2008 /SQL Server 2008 R2 配置数据库邮件
查看>>
[转]vs2010编译金山代码
查看>>
数学图形之Boy surface
查看>>
处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“Manag
查看>>
3.浏览器检测
查看>>
01: socket模块
查看>>
Border-radius
查看>>
mysql触发器
查看>>
Redis学习笔记(1)Redis安装和启动
查看>>
淌淌淌
查看>>
MySQL-定时任务
查看>>
web页面实现指定区域打印功能
查看>>
使用PHP拆分中文字符串的方法(收藏) 小节
查看>>
android系统权限的管理
查看>>
win10每次开机都显示“你的硬件设置已更改,请重启电脑……”的解决办法
查看>>
因Window服务器自动更新并重启导致WebSphere服务停止服务故障一例
查看>>
如何开启safari的调试
查看>>