1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.util; |
8 | |
|
9 | |
import org.w3c.dom.Attr; |
10 | |
import org.w3c.dom.Element; |
11 | |
import org.w3c.dom.Node; |
12 | |
import org.w3c.dom.NodeList; |
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | 0 | public class XMLUtils |
19 | |
{ |
20 | |
|
21 | |
public static String elementToString(Element e) |
22 | |
{ |
23 | 0 | StringBuffer buf = new StringBuffer(); |
24 | 0 | buf.append(e.getTagName()).append("{"); |
25 | 0 | for (int i = 0; i < e.getAttributes().getLength(); i++) |
26 | |
{ |
27 | 0 | if (i > 0) |
28 | |
{ |
29 | 0 | buf.append(", "); |
30 | |
} |
31 | 0 | Node n = e.getAttributes().item(i); |
32 | 0 | buf.append(attributeName((Attr) n)).append("=").append(n.getNodeValue()); |
33 | |
} |
34 | 0 | buf.append("}"); |
35 | 0 | return buf.toString(); |
36 | |
} |
37 | |
|
38 | |
public static boolean isLocalName(Element element, String name) |
39 | |
{ |
40 | 0 | return element.getLocalName().equals(name); |
41 | |
} |
42 | |
|
43 | |
public static String attributeName(Attr attribute) |
44 | |
{ |
45 | 0 | String name = attribute.getLocalName(); |
46 | 0 | if (null == name) |
47 | |
{ |
48 | 0 | name = attribute.getName(); |
49 | |
} |
50 | 0 | return name; |
51 | |
} |
52 | |
|
53 | |
public static String getTextChild(Element element) |
54 | |
{ |
55 | 0 | NodeList children = element.getChildNodes(); |
56 | 0 | String value = null; |
57 | 0 | for (int i = 0; i < children.getLength(); ++i) |
58 | |
{ |
59 | 0 | Node child = children.item(i); |
60 | 0 | if (child.getNodeType() == Node.TEXT_NODE) |
61 | |
{ |
62 | 0 | if (null != value) |
63 | |
{ |
64 | 0 | throw new IllegalStateException( |
65 | |
"Element " + elementToString(element) + " has more than one text child."); |
66 | |
} |
67 | |
else |
68 | |
{ |
69 | 0 | value = child.getNodeValue(); |
70 | |
} |
71 | |
} |
72 | |
} |
73 | 0 | return value; |
74 | |
} |
75 | |
|
76 | |
} |