1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| package org.ericmignot.modeler.util.xml; |
20 |
| |
21 |
| import java.io.*; |
22 |
| import javax.xml.parsers.*; |
23 |
| import org.w3c.dom.*; |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| public class XMLHelper |
29 |
| { |
30 |
0
| public static Element getFirstElementByName(Element element, String tagName)
|
31 |
| { |
32 |
0
| return toto(element, tagName);
|
33 |
| } |
34 |
| |
35 |
106
| public static Element getFirstElementByName(String xml, String tagName)
|
36 |
| { |
37 |
106
| try
|
38 |
| { |
39 |
106
| DocumentBuilderFactory l_Factory =
|
40 |
| DocumentBuilderFactory.newInstance(); |
41 |
106
| DocumentBuilder l_DocumentBuilder = l_Factory.newDocumentBuilder();
|
42 |
106
| Document l_Document = l_DocumentBuilder.parse(
|
43 |
| new ByteArrayInputStream(xml.getBytes())); |
44 |
106
| Element l_Element = l_Document.getDocumentElement();
|
45 |
| |
46 |
106
| return toto(l_Element, tagName);
|
47 |
| } |
48 |
| catch (Exception e) |
49 |
| { |
50 |
0
| e.printStackTrace();
|
51 |
0
| return null;
|
52 |
| } |
53 |
| } |
54 |
| |
55 |
106
| private static Element toto(Element p_CurrentElement, String p_TagName)
|
56 |
| { |
57 |
106
| if (p_CurrentElement != null)
|
58 |
| { |
59 |
106
| if (p_CurrentElement.getNodeName().equalsIgnoreCase(p_TagName))
|
60 |
| { |
61 |
105
| return p_CurrentElement;
|
62 |
| } |
63 |
1
| NodeList l_Childs = p_CurrentElement.getChildNodes();
|
64 |
1
| for (int i = 0; i < l_Childs.getLength(); i ++)
|
65 |
| { |
66 |
1
| Node l_Child = l_Childs.item(i);
|
67 |
1
| if (l_Child instanceof Element)
|
68 |
| { |
69 |
0
| Element found = toto((Element) l_Child, p_TagName);
|
70 |
0
| if (found != null)
|
71 |
| { |
72 |
0
| return found;
|
73 |
| } |
74 |
| } |
75 |
| } |
76 |
| } |
77 |
1
| return null;
|
78 |
| } |
79 |
| |
80 |
0
| public static String formatXML(String source)
|
81 |
| { |
82 |
0
| String toReturn = "";
|
83 |
0
| int tabNumber = 0;
|
84 |
0
| for (int i=0; i<source.length(); i++)
|
85 |
| { |
86 |
0
| if ("<".equalsIgnoreCase(source.substring(i, i+1)))
|
87 |
| { |
88 |
0
| if ((i<(source.length()-2)) && "</".equalsIgnoreCase(source.substring(i, i+2)))
|
89 |
| { |
90 |
0
| tabNumber --;
|
91 |
0
| toReturn += "\n";
|
92 |
0
| toReturn = addTabs(toReturn, tabNumber);
|
93 |
| } |
94 |
| else |
95 |
| { |
96 |
0
| tabNumber ++;
|
97 |
0
| toReturn += "\n";
|
98 |
0
| toReturn = addTabs(toReturn, tabNumber);
|
99 |
| } |
100 |
| } |
101 |
0
| if ((i<(source.length()-2)) && "/>".equalsIgnoreCase(source.substring(i, i+2)))
|
102 |
| { |
103 |
0
| tabNumber --;
|
104 |
| } |
105 |
0
| toReturn += source.substring(i, i+1);
|
106 |
| } |
107 |
0
| return toReturn;
|
108 |
| } |
109 |
| |
110 |
0
| public static String addTabs(String source, int nbTab)
|
111 |
| { |
112 |
0
| String toReturn = new String(source);
|
113 |
0
| for (int j=0; j<nbTab; j++)
|
114 |
| { |
115 |
0
| toReturn += "\t";
|
116 |
| } |
117 |
0
| return toReturn;
|
118 |
| } |
119 |
| } |