2
2
3
3
import java .io .File ;
4
4
import java .net .URL ;
5
+ import java .util .ArrayList ;
6
+ import java .util .List ;
5
7
6
8
import javax .xml .parsers .DocumentBuilder ;
7
9
import javax .xml .parsers .DocumentBuilderFactory ;
@@ -20,30 +22,20 @@ public class StrutsUtil {
20
22
21
23
public final static String CONFIG_ATTR_CLASS = "class" ;
22
24
23
- public Node invokedAction (String actionName ){
25
+ public String invokedAction (String actionName ){
24
26
if (null != actionName && !"" .equals (actionName )){
25
27
Document doc = generateDoc ();
26
28
NodeList nodeList = doc .getElementsByTagName (CONFIG_NODE_ACTION );
27
29
for (int i = 0 ; i < nodeList .getLength (); i ++) {
28
30
String actionNameConfiged = nodeList .item (i ).getAttributes ().getNamedItem (CONFIG_ATTR_NAME ).getNodeValue ();
29
31
if (actionName .equals (actionNameConfiged )){
30
- return nodeList .item (i ).getAttributes ().getNamedItem (CONFIG_ATTR_CLASS );
32
+ return nodeList .item (i ).getAttributes ().getNamedItem (CONFIG_ATTR_CLASS ). getNodeValue () ;
31
33
}
32
34
}
33
35
}
34
36
throw new RuntimeException ("actionName can't be found" );
35
37
}
36
38
37
-
38
- public void readXML (){
39
- Document doc = generateDoc ();
40
- NodeList nodeList = doc .getElementsByTagName ("result" );
41
- System .out .println (nodeList .item (0 ).getParentNode ().getAttributes ().getNamedItem (CONFIG_ATTR_NAME ).getNodeValue ());
42
- System .out .println (nodeList .item (0 ).getAttributes ().getNamedItem (CONFIG_ATTR_NAME ).getNodeValue ());
43
- System .out .println (nodeList .item (0 ).getTextContent ());
44
- System .out .println ("Read XML Finished" );
45
- }
46
-
47
39
private Document generateDoc (){
48
40
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory .newInstance ();
49
41
Document doc = null ;
@@ -60,9 +52,25 @@ private File lookupConfigFile(){
60
52
URL url = getClass ().getResource (CONFIG_PATH );
61
53
return new File (url .getPath ());
62
54
}
63
-
64
- public static void main (String [] args ) {
65
- new StrutsUtil ().readXML ();
55
+
56
+
57
+ public String invokeResult (String actionName , String result ) {
58
+ Document doc = generateDoc ();
59
+ NodeList nodeList = doc .getElementsByTagName ("result" );
60
+ List <Node > subNodeList = new ArrayList <Node >();
61
+ for (int i = 0 ; i < nodeList .getLength (); i ++) {
62
+ Node parentNode = nodeList .item (i ).getParentNode ();
63
+ if (parentNode .getAttributes ().getNamedItem (CONFIG_ATTR_NAME ).getNodeValue ().equals (actionName )){
64
+ subNodeList .add (nodeList .item (i ));
65
+ }
66
+ }
67
+ for (int i = 0 ; i < subNodeList .size (); i ++) {
68
+ Node node = subNodeList .get (i );
69
+ if (node .getAttributes ().getNamedItem (CONFIG_ATTR_NAME ).getNodeValue ().equals (result )){
70
+ return node .getTextContent ();
71
+ }
72
+ }
73
+ throw new RuntimeException ("result can't be found" );
66
74
}
67
75
68
76
}
0 commit comments