Commit 46e7c663 authored by Christopher Cottier's avatar Christopher Cottier

get node by name path method

parent a80e62bc
...@@ -5,19 +5,25 @@ import groovy.xml.slurpersupport.GPathResult ...@@ -5,19 +5,25 @@ import groovy.xml.slurpersupport.GPathResult
class XMLParser { class XMLParser {
public File file; File file;
GPathResult xmlNodes;
static void main(String[] args) { public void loadFile(String fileLocation){
file = new File(fileLocation);
}
public GPathResult createParser() {
return new XmlSlurper().parse(file)
} }
public void loadFile(String fileLocation){ public GPathResult getNodeByPath(String[] path){
file = new File(fileLocation); GPathResult node = createParser()
for (String nodeName : path) {
node = node."${nodeName}"
} }
public void createParser() { return node;
xmlNodes = new XmlSlurper().parse(file)
} }
......
...@@ -2,30 +2,20 @@ package com.nisum ...@@ -2,30 +2,20 @@ package com.nisum
import groovy.xml.slurpersupport.GPathResult import groovy.xml.slurpersupport.GPathResult
//import groovy.xml.XmlSlurper String fileLocation = "./web.xml"
//
//def webApp = new XmlSlurper().parse("./web.xml")
//assert webApp instanceof groovy.util.Node
//String fileLocation = "./web.xml" //eventually script will require absolute path from user input
String fileLocation = "/Users/ccottier/Documents/ds-spring-conversion-tool/spring-conversion-xml-parser/src/main/groovy/com/nisum/web.xml" //String fileLocation = "/Users/ccottier/Documents/ds-spring-conversion-tool/spring-conversion-xml-parser/src/main/groovy/com/nisum/web.xml"
XMLParser parser = new XMLParser() XMLParser parser = new XMLParser()
parser.loadFile(fileLocation) parser.loadFile(fileLocation)
parser.createParser()
GPathResult webApp = parser.xmlNodes String[] servPath = ["servlet","servlet-name"]
def listenerNode = webApp.'listener' def servletNameNode = parser.getNodeByPath(servPath)
def contextParamNode = webApp.'context-param'
def servletNode = webApp.'servlet'
def servletMappingNode = webApp.'servlet-mapping'
//HashMap<String,String> hm = new HashMap<>(); println(servletNameNode.toString())
println(servletNode.'servlet-name'.toString())
def listenerClass2 = webApp['@version']
//def listenerClass2 = webApp['@version']
//println(listenerClass2) //println(listenerClass2)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment