Statistics
| Revision:

root / tmp / org.txm.core / src / java / org / txm / scripts / importer / GetAttributeValue.groovy @ 1688

History | View | Annotate | Download (1.6 kB)

1
package org.txm.scripts.importer
2

    
3
import java.util.HashMap;
4
import java.util.ArrayList;
5
import java.io.File;
6
import javax.xml.stream.*;
7
import java.net.URL;
8

    
9
class GetAttributeValue {
10
        public static String process(File xmlFile, String path, String attribute) {
11
                String value = "N/A";
12
                String currentpath = "";
13
                boolean done = false;
14
                
15
                def inputData = null;
16
                def factory = null;
17
                XMLStreamReader parser = null;
18
                try {
19
                        URL url = xmlFile.toURI().toURL();
20
                        inputData = url.openStream();
21
                        factory = XMLInputFactory.newInstance();
22
                        parser = factory.createXMLStreamReader(inputData);
23
                        for (int event = parser.next(); !done && event != XMLStreamConstants.END_DOCUMENT; event = parser.next())
24
                        {
25
                                switch (event) {
26
                                        case XMLStreamConstants.START_ELEMENT:
27
                                                currentpath += "/"+parser.getLocalName();// append the current tag to the current path
28
                                                if (path == currentpath) {
29
                                                        for (int i = 0 ; i < parser.getAttributeCount() ; i++) {
30
                                                                if (parser.getAttributeName(i) == attribute) {
31
                                                                        value = parser.getAttributeValue(i)
32
                                                                        done = true;
33
                                                                        break;
34
                                                                }
35
                                                        }
36
                                                }
37
                                                
38
                                                break;
39
                                        case XMLStreamConstants.END_ELEMENT:
40
                                                currentpath = currentpath.substring(0,currentpath.length() -1 - parser.getLocalName().length()) // remove tag from the path
41
                                                break;
42
                                        
43
                                }
44
                        }
45
                }
46
                catch(Exception e){println("File "+xmlFile+"\n"+e); 
47
                        if (parser != null) parser.close();
48
                        if (inputData != null) inputData.close();
49
                        return "N/A";}
50
                
51
                if (parser != null) parser.close();
52
                if (inputData != null) inputData.close();
53
                return value;
54
        }
55
}