Skip to content

Commit 2a997e8

Browse files
committed
重新提交添加XmlUtils工具类
1 parent 60f6633 commit 2a997e8

File tree

6 files changed

+250
-0
lines changed

6 files changed

+250
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package iqq.app.util;
2+
3+
import org.dom4j.Document;
4+
import org.dom4j.DocumentException;
5+
import org.dom4j.Element;
6+
import org.dom4j.Node;
7+
import org.dom4j.io.OutputFormat;
8+
import org.dom4j.io.SAXReader;
9+
import org.dom4j.io.XMLWriter;
10+
11+
import java.io.File;
12+
import java.io.FileOutputStream;
13+
import java.io.IOException;
14+
import java.util.HashMap;
15+
import java.util.Map;
16+
17+
/**
18+
* XML操作类 读取/写入/获取值
19+
*
20+
* Project : iqq
21+
* Author : 承∮诺 < [email protected] >
22+
* Created : 14-4-16
23+
* License : Apache License 2.0
24+
*/
25+
public class XmlUtils {
26+
private static Map<String, Document> xmlCache = new HashMap<String, Document>();
27+
28+
/**
29+
* 获取XML节点的值
30+
*
31+
* @param filename
32+
* @param key
33+
* @return
34+
* @throws DocumentException
35+
*/
36+
public static String getNodeText(String filename, String key) throws DocumentException {
37+
return getNode(filename, key).getText();
38+
}
39+
40+
/**
41+
* 获取XML节点
42+
*
43+
* @param filename
44+
* @param key
45+
* @return
46+
* @throws DocumentException
47+
*/
48+
public static Node getNode(String filename, String key) throws DocumentException {
49+
return getElement(filename).selectSingleNode(key);
50+
}
51+
52+
/**
53+
* 获取XML元素
54+
*
55+
* @param filename
56+
* @return
57+
* @throws DocumentException
58+
*/
59+
public static Element getElement(String filename) throws DocumentException {
60+
return readXml(filename).getRootElement();
61+
}
62+
63+
/**
64+
* 读入XML文件
65+
*
66+
* @param filename
67+
* @return
68+
* @throws DocumentException
69+
*/
70+
public static Document readXml(String filename) throws DocumentException {
71+
Document document = null;
72+
if(xmlCache.get(filename) == null) {
73+
// 创建SAXReader读取器
74+
SAXReader saxReader = new SAXReader();
75+
// 读取xml
76+
document = saxReader.read(new File(filename));
77+
// 放到缓存中
78+
xmlCache.put(filename, document);
79+
}
80+
return document;
81+
}
82+
83+
/**
84+
* 写入XML文件
85+
*
86+
* @param document
87+
* @param xmlFile
88+
* @throws IOException
89+
*/
90+
public static void writeXml(Document document, String filename) throws IOException {
91+
OutputFormat outputFormat = OutputFormat.createPrettyPrint();// 设置XML文档输出格式
92+
outputFormat.setEncoding("UTF-8");// 设置XML文档的编码类型
93+
outputFormat.setIndent(true);// 设置是否缩进
94+
outputFormat.setIndent(" ");// 以TAB方式实现缩进
95+
outputFormat.setNewlines(true);// 设置是否换行
96+
XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(filename), outputFormat);
97+
xmlWriter.write(document);
98+
xmlWriter.close();
99+
}
100+
101+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package iqq.app;
2+
3+
import iqq.app.core.context.IMContext;
4+
import iqq.app.core.service.SkinService;
5+
import iqq.app.core.service.impl.SkinServiceImpl;
6+
import junit.framework.Assert;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
/**
11+
* Project : iqq
12+
* Author : 承∮诺 < [email protected] >
13+
* Created : 14-4-16
14+
* License : Apache License 2.0
15+
*/
16+
public class TestIoc {
17+
@Before
18+
public void before() {
19+
IMContext.getIoc();
20+
}
21+
22+
@Test
23+
public void testBean() {
24+
SkinService skinService = IMContext.getIoc().get(SkinServiceImpl.class);
25+
Assert.assertNotNull(skinService);
26+
}
27+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package iqq.app;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* Project : iqq
7+
* Author : 承∮诺 < [email protected] >
8+
* Created : 14-4-16
9+
* License : Apache License 2.0
10+
*/
11+
public class TestPath {
12+
@Test
13+
public void testProjectDir() {
14+
System.out.println(System.getProperty("user.dir"));
15+
}
16+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package iqq.app.service;
2+
3+
import iqq.app.core.service.SkinService;
4+
import iqq.app.core.service.impl.SkinServiceImpl;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
import java.io.File;
9+
10+
/**
11+
* Project : iqq
12+
* Author : 承∮诺 < [email protected] >
13+
* Created : 14-4-16
14+
* License : Apache License 2.0
15+
*/
16+
public class TestSkinService {
17+
@Test
18+
public void testPath() {
19+
//System.setProperty("app.dir", "");
20+
SkinService skinService = new SkinServiceImpl();
21+
String path = skinService.getDirectory();
22+
System.out.println(path);
23+
Assert.assertNotNull(path);
24+
Assert.assertTrue(new File(path).exists());
25+
}
26+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package iqq.app.ui;
2+
3+
import com.alee.laf.WebLookAndFeel;
4+
import iqq.app.core.context.IMContext;
5+
import iqq.app.ui.frame.LoginFrame;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
9+
import java.util.Scanner;
10+
11+
/**
12+
* Project : iqq
13+
* Author : 承∮诺 < [email protected] >
14+
* Created : 14-4-16
15+
* License : Apache License 2.0
16+
*/
17+
public class TestLogin {
18+
19+
@Before
20+
public void before() {
21+
IMContext.getIoc();
22+
WebLookAndFeel.install();
23+
}
24+
25+
@Test
26+
public void testLoginBg() {
27+
LoginFrame login = new LoginFrame();
28+
new Scanner(System.in).next();
29+
}
30+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package iqq.app.util;
2+
3+
import org.dom4j.Document;
4+
import org.dom4j.DocumentException;
5+
import org.dom4j.DocumentHelper;
6+
import org.dom4j.Element;
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
import java.io.IOException;
11+
12+
/**
13+
* Project : iqq
14+
* Author : 承∮诺 < [email protected] >
15+
* Created : 14-4-16
16+
* License : Apache License 2.0
17+
*/
18+
public class TestXml {
19+
20+
@Test
21+
public void testWrite() {
22+
String xmlFile = System.getProperty("user.dir") + "/abc.xml";
23+
Document doc = DocumentHelper.createDocument();
24+
25+
Element root = doc.addElement("roots");
26+
root.addElement("test").setText("test123");
27+
28+
Element element = root.addElement("iqq");
29+
element.addElement("version").setText("iqqv3");
30+
31+
// 测试XmlUtils
32+
try {
33+
XmlUtils.writeXml(doc, xmlFile);
34+
} catch (IOException e) {
35+
e.printStackTrace();
36+
}
37+
}
38+
39+
@Test
40+
public void testRead() {
41+
String xmlFile = System.getProperty("user.dir") + "/abc.xml";
42+
try {
43+
String text = XmlUtils.getNodeText(xmlFile, "iqq/version");
44+
System.out.println(text);
45+
Assert.assertNotNull(text);
46+
} catch (DocumentException e) {
47+
e.printStackTrace();
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)