提交 | 用户 | age
|
5c5945
|
1 |
package com.hx.mp.util; |
E |
2 |
|
|
3 |
import org.jdom.Document; |
|
4 |
import org.jdom.Element; |
|
5 |
import org.jdom.JDOMException; |
|
6 |
import org.jdom.input.SAXBuilder; |
|
7 |
|
|
8 |
import java.io.ByteArrayInputStream; |
|
9 |
import java.io.IOException; |
|
10 |
import java.io.InputStream; |
|
11 |
import java.util.HashMap; |
|
12 |
import java.util.Iterator; |
|
13 |
import java.util.List; |
|
14 |
import java.util.Map; |
|
15 |
|
|
16 |
/** |
|
17 |
* xml������ |
|
18 |
*JX |
|
19 |
*linhan |
|
20 |
*/ |
|
21 |
public class XMLUtil { |
|
22 |
|
|
23 |
/** |
|
24 |
* ����xml,���ص�һ��Ԫ�ؼ�ֵ�ԡ�����һ��Ԫ�����ӽڵ㣬��˽ڵ��ֵ���ӽڵ��xml��ݡ� |
|
25 |
* @param strxml |
|
26 |
* @return |
|
27 |
* @throws JDOMException |
|
28 |
* @throws IOException |
|
29 |
*/ |
|
30 |
@SuppressWarnings({ "rawtypes", "unchecked" }) |
|
31 |
public static Map doXMLParse(String strxml) throws JDOMException, IOException { |
|
32 |
strxml = strxml.replaceFirst("encoding=\".*\"", "encoding=\"UTF-8\""); |
|
33 |
|
|
34 |
if(null == strxml || "".equals(strxml)) { |
|
35 |
return null; |
|
36 |
} |
|
37 |
|
|
38 |
Map m = new HashMap(); |
|
39 |
|
|
40 |
InputStream in = new ByteArrayInputStream(strxml.getBytes("UTF-8")); |
|
41 |
SAXBuilder builder = new SAXBuilder(); |
|
42 |
Document doc = builder.build(in); |
|
43 |
Element root = doc.getRootElement(); |
|
44 |
List list = root.getChildren(); |
|
45 |
Iterator it = list.iterator(); |
|
46 |
while(it.hasNext()) { |
|
47 |
Element e = (Element) it.next(); |
|
48 |
String k = e.getName(); |
|
49 |
String v = ""; |
|
50 |
List children = e.getChildren(); |
|
51 |
if(children.isEmpty()) { |
|
52 |
v = e.getTextNormalize(); |
|
53 |
} else { |
|
54 |
v = XMLUtil.getChildrenText(children); |
|
55 |
} |
|
56 |
|
|
57 |
m.put(k, v); |
|
58 |
} |
|
59 |
|
|
60 |
//�ر��� |
|
61 |
in.close(); |
|
62 |
|
|
63 |
return m; |
|
64 |
} |
|
65 |
|
|
66 |
/** |
|
67 |
* ��ȡ�ӽ���xml |
|
68 |
* @param children |
|
69 |
* @return String |
|
70 |
*/ |
|
71 |
@SuppressWarnings("rawtypes") |
|
72 |
public static String getChildrenText(List children) { |
|
73 |
StringBuffer sb = new StringBuffer(); |
|
74 |
if(!children.isEmpty()) { |
|
75 |
Iterator it = children.iterator(); |
|
76 |
while(it.hasNext()) { |
|
77 |
Element e = (Element) it.next(); |
|
78 |
String name = e.getName(); |
|
79 |
String value = e.getTextNormalize(); |
|
80 |
List list = e.getChildren(); |
|
81 |
sb.append("<" + name + ">"); |
|
82 |
if(!list.isEmpty()) { |
|
83 |
sb.append(XMLUtil.getChildrenText(list)); |
|
84 |
} |
|
85 |
sb.append(value); |
|
86 |
sb.append("</" + name + ">"); |
|
87 |
} |
|
88 |
} |
|
89 |
|
|
90 |
return sb.toString(); |
|
91 |
} |
|
92 |
|
|
93 |
/** |
|
94 |
* ��ȡxml�����ַ� |
|
95 |
* @param strxml |
|
96 |
* @return |
|
97 |
* @throws IOException |
|
98 |
* @throws JDOMException |
|
99 |
*/ |
|
100 |
public static String getXMLEncoding(String strxml) throws JDOMException, IOException { |
|
101 |
InputStream in = HttpClientUtil.String2Inputstream(strxml); |
|
102 |
SAXBuilder builder = new SAXBuilder(); |
|
103 |
Document doc = builder.build(in); |
|
104 |
in.close(); |
|
105 |
return (String)doc.getProperty("encoding"); |
|
106 |
} |
|
107 |
|
|
108 |
|
|
109 |
} |