Statistics
| Revision:

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

History | View | Annotate | Download (1.4 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
                try {
16
                        URL url = xmlFile.toURI().toURL();
17
                        def inputData = url.openStream();
18
                        def factory = XMLInputFactory.newInstance();
19
                        XMLStreamReader parser = factory.createXMLStreamReader(inputData);
20
                        for (int event = parser.next(); !done && event != XMLStreamConstants.END_DOCUMENT; event = parser.next())
21
                        {
22
                                switch (event) {
23
                                        case XMLStreamConstants.START_ELEMENT:
24
                                                currentpath += "/"+parser.getLocalName();// append the current tag to the current path
25
                                                if (path == currentpath) {
26
                                                        for (int i = 0 ; i < parser.getAttributeCount() ; i++) {
27
                                                                if (parser.getAttributeName(i) == attribute) {
28
                                                                        value = parser.getAttributeValue(i)
29
                                                                        done = true;
30
                                                                        break;
31
                                                                }
32
                                                        }
33
                                                }
34
                                                
35
                                                break;
36
                                        case XMLStreamConstants.END_ELEMENT:
37
                                                currentpath = currentpath.substring(0,currentpath.length() -1 - parser.getLocalName().length()) // remove tag from the path
38
                                                break;
39
                                        
40
                                }
41
                        }
42
                        
43
                }
44
                catch(Exception e){println("File "+xmlFile+"\n"+e); return "N/A";}
45
                return value;
46
        }
47
}