2014-01-27 3 views
0

groovy에서 XMLSlurper를 통해 xml을 구문 분석하는 이상한 문제가 발생했으며 크기를 0으로 표시합니다. 이유를 파악할 수 없습니다.XML을 통해 Groovy에서 XML 구문 분석

내 XML 파일과 같이 보인다 :

<?xml version="1.0" encoding="iso-8859-1"?> 
<sites> 
    <site name="OctUK"> 
     <property name="warName">OctUKbuild-Deployable</property> 
    </site> 
    <site name="GbsJP"> 
     <property name="warName">GbsJPbuild-Deployable</property> 
    </site> 
</sites> 

코드 :이 0으로 siteGPath의 결과를 제공하기 때문에 어떤 사이트가 발견되지 :

findSite("${project.GTA_BUILD_HOME}/platforms/pos/config/pos-sites.xml") 

//Passed the path of the xml file to the method below: 
GPathResult findSite(String sitesXml) { 
    xmlConfig = new XmlSlurper().parse(new File(sitesXml)) 
    def siteGPath = xmlConfig.sites.site.findAll 
    // Check that a POS-sites.xml is valid 
    assert siteGPath.size() != 0, 'Error: no site found' 
    return(siteGPath) 
} 

방법은 오류가 오류라고 실패합니다. 왜 결과가 0인지 알려지지 않습니다. 크기는 2로 지정해야합니다.

잘못된 것이 있습니까? 어떤 도움이라도 대단히 감사합니다. 나는이 시점에서 붙어있다.

답변

1

xmlConfig 개체를 볼 때 sites이 필요하지 않습니다.

assert xmlConfig.site.size() == 2 

또한, xmlConfig.siteNodeChildren의 인스턴스가,하지만 당신은 GPathResult

의 반환 유형을 선언 할 것으로 보인다 그리고 나는 ':

sites

, 그래서 암시 루트 노드, 시도 findAll 통화가 끝날 때 누락 된 것이 확실하지 않으므로 폐쇄가 필요하거나 빈 괄호가 필요합니다.

+0

감사합니다. Tim. 나는 이제 siteGPath = xmlConfig.site를 def로 내 코드를 수정했다. – user1470220