XmlElementFactory.java
1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.ectrip.cyt.exceptionsave.xml;
import java.io.File;
import java.io.InputStream;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class XmlElementFactory {
//防止被实例
private XmlElementFactory(){};
//读取流
public static Element decodeRoot(InputStream in) {
try {
SAXReader reader = new SAXReader();
Document doc = reader.read(in);
Element root = doc.getRootElement();
return root;
} catch (DocumentException e) {
e.printStackTrace();
}
return null;
}
//读取流
public static Element decodeRoot(File f) {
try {
SAXReader reader = new SAXReader();
Document doc = reader.read(f);
Element root = doc.getRootElement();
return root;
} catch (DocumentException e) {
e.printStackTrace();
}
return null;
}
public static Element decodeRoot(String xmlStr)
{
try {
Document doc = DocumentHelper.parseText(xmlStr);
Element root=doc.getRootElement();
return root;
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static Element decodeRootResource(String classResourcePath)
{
return decodeRoot(XmlElementFactory.class.getResourceAsStream(classResourcePath));
}
}