|
1 | 1 | package com;
|
2 | 2 |
|
3 |
| -import org.dom4j.Attribute; |
4 |
| -import org.dom4j.Document; |
5 |
| -import org.dom4j.Element; |
| 3 | +import org.dom4j.*; |
| 4 | +import org.dom4j.io.OutputFormat; |
6 | 5 | import org.dom4j.io.SAXReader;
|
| 6 | +import org.dom4j.io.XMLWriter; |
7 | 7 |
|
8 |
| -import java.io.File; |
| 8 | +import java.io.*; |
9 | 9 |
|
10 | 10 | /**
|
11 | 11 | * Created by lenovo on 二月
|
12 | 12 | */
|
13 | 13 | public class XmlUtils {
|
14 |
| - public void getXmlContent() throws Exception { |
15 |
| - SAXReader saxReader=new SAXReader(); |
16 |
| - //获取文件,转换成Document对象 |
17 |
| - Document document=saxReader.read( new File("E:\\test.xml")); |
18 |
| - //获取根元素 |
19 |
| - Element root=document.getRootElement(); |
20 |
| - //文档转换为字符串 |
21 |
| - String docXmlText=document.asXML(); |
22 |
| - System.out.println("xml内容如下:"); |
23 |
| - System.out.println(docXmlText); |
24 |
| - System.out.println("根目录标签的内容如下:"); |
25 |
| - String rootXmlText=root.asXML(); |
26 |
| - System.out.println(rootXmlText); |
27 |
| - System.out.println("子标签<西游记>的内容如下:"); |
28 |
| - Element e1=root.element("西游记"); |
29 |
| - System.out.println(e1.asXML()); |
30 |
| - System.out.println("子标签<作者>的内容如下:"); |
31 |
| - Element e2=e1.element("作者"); |
32 |
| - System.out.println(e2.asXML()); |
33 |
| - System.out.println("标签内容如下:"); |
34 |
| - String text=e2.getText(); |
35 |
| -// String text= e2.asXML().substring(e2.asXML().indexOf(">") + 1, e2.asXML().lastIndexOf("<")); |
36 |
| - System.out.println(text); |
37 |
| - } |
| 14 | + /** |
| 15 | + * 获取xml文件的内容 |
| 16 | + * @param fileName 文件名,类似 String fileName1="E:\\test.xml"; |
| 17 | + * @param names 标签组成的字符数组.比如 String[] names=new String[]{"西游记","作者"}; |
| 18 | + * @throws Exception |
| 19 | + */ |
| 20 | + public void getXmlContent(String fileName,String[] names) throws Exception { |
| 21 | + SAXReader saxReader=new SAXReader(); |
| 22 | + //获取文件,转换成Document对象 |
| 23 | + Document document=saxReader.read( new File(fileName)); |
| 24 | + //获取xml内容 |
| 25 | + String docXmlText=document.asXML(); |
| 26 | + System.out.println("xml内容如下:"+docXmlText); |
| 27 | + System.out.println("-----------------------------"); |
| 28 | + //获取根元素 |
| 29 | + Element e=document.getRootElement(); |
| 30 | + //遍历xml文档 |
| 31 | + for(String name : names) { |
| 32 | + if ( e!=null && e.element(name)!=null) { |
| 33 | + e=e.element(name); |
| 34 | + String elementStr=e.asXML(); |
| 35 | + System.out.println(elementStr); |
| 36 | + System.out.println("-----------------------------"); |
| 37 | + } |
| 38 | + } |
| 39 | + String text=e.getText(); |
| 40 | + System.out.println("元素内容如下:\n"+text); |
| 41 | + } |
38 | 42 |
|
39 | 43 |
|
40 |
| - public void getElementText() throws Exception{ |
41 |
| - SAXReader saxReader=new SAXReader(); |
42 |
| - //获取文件,转换成Document对象 |
43 |
| - Document document=saxReader.read( new File("E:\\test.xml")); |
44 |
| - Element root=document.getRootElement(); |
45 |
| - Attribute attribute=root.attribute("四大名著"); |
46 |
| - String text=attribute.getText(); |
47 |
| - System.out.println(text); |
| 44 | + /** |
| 45 | + * 字符串写入xml文件 |
| 46 | + * @param text 文本内容,类似 String text="<csdn><java>Java班</java><net>Net班</net></csdn>"; |
| 47 | + * @throws Exception |
| 48 | + */ |
| 49 | + public void stringToXml(String text,String fileName) throws Exception { |
| 50 | + Document document= DocumentHelper.parseText(text); |
| 51 | + Element element=document.getRootElement(); |
| 52 | + System.out.println(element.getName()); |
| 53 | + writeToXml(document,fileName); |
| 54 | + } |
48 | 55 |
|
| 56 | + /** |
| 57 | + * 把document对象写入xml文件中 |
| 58 | + * @param document |
| 59 | + */ |
| 60 | + public void writeToXml(Document document,String fileName) throws Exception { |
| 61 | +// 紧凑的格式 |
| 62 | +// OutputFormat format=OutputFormat.createCompactFormat(); |
| 63 | +// 排版缩进的格式 |
| 64 | + OutputFormat format=OutputFormat.createPrettyPrint(); |
| 65 | + format.setEncoding("UTF-8"); |
| 66 | + //创建XmlWriter对象,指定文件及编码 |
| 67 | + XMLWriter xmlWriter=new XMLWriter(new OutputStreamWriter(new FileOutputStream(new File(fileName)),"UTF-8"),format); |
| 68 | + xmlWriter.write(document); |
| 69 | + //刷新流 |
| 70 | + xmlWriter.flush(); |
| 71 | + xmlWriter.close(); |
| 72 | + } |
49 | 73 |
|
| 74 | + public void createDocumentToXml(String fileName) throws Exception{ |
| 75 | + Document document=DocumentHelper.createDocument(); |
| 76 | + Element root=document.addElement("csdn"); |
| 77 | + Element java=root.addElement("java"); |
| 78 | + java.setText("java班"); |
| 79 | + Element ios=root.addElement("ios"); |
| 80 | + ios.setText("ios班"); |
| 81 | + writeToXml(document,fileName); |
| 82 | + } |
50 | 83 |
|
51 |
| - } |
| 84 | +// public void getElementText() throws Exception{ |
| 85 | +// SAXReader saxReader=new SAXReader(); |
| 86 | +// //获取文件,转换成Document对象 |
| 87 | +// Document document=saxReader.read( new File("E:\\test.xml")); |
| 88 | +// Element root=document.getRootElement(); |
| 89 | +// Attribute attribute=root.attribute("四大名著"); |
| 90 | +// String text=attribute.getText(); |
| 91 | +// System.out.println(text); |
| 92 | +// |
| 93 | +// |
| 94 | +// |
| 95 | +// } |
52 | 96 | }
|
0 commit comments