使用Groovy xml Building创建嵌套节点

时间:2011-11-02 06:10:11

标签: java groovy

我有以下xml,更新已添加的“症状”

<EBF>
<EBFINFO>
    <EBFNUM>EBF262323</EBFNUM>  
    <RELEASEDATETIME>May 06, 2011</RELEASEDATETIME>
    <SYMPTOMS>
    <br> INFA252994 - 910 : While running concurrent session Workflow manager hangs and workflow monitor does not respond</br>
    <br> INFA262323 - 910 : pmcmd, pmdtm and all LM clients on Windows fail to connect to IS when IPv6 is installed but all IPv6 interfaces are disabled</br>
    </SYMPTOMS>
    <FILES>
       <FILE>
        <PATH>H:\EBF262323\EBF262323_Client_Installer_win32_x86\EBFs\clients\PmClient\client\bin\ACE.dll_bak</PATH>
        <CHECKSUM>303966974</CHECKSUM>
        <AFFECTEDFILES>
            <CHECKSUM>3461283269</CHECKSUM>
            <PATH>C:\clients\PmClient\CommandLineUtilities\PC\server\bin\ACE.dll</PATH>
            <PATH>C:\clients\PmClient\client\bin\ACE.dll</PATH>
        </AFFECTEDFILES>
       </FILE>
    </FILES>
    <NOTES>
    </NOTES>
</EBFINFO>
</EBF>

注意:在上面的xml中,ebf \ enfinfo \ files \ file \ affectedfiles \ path和ebf \ enfinfo \ files \ file可以是一个或多个

我正在解析并从中生成另一个xml

    def records = new XmlParser().parseText(rs)
    csm.ebfHistory(){
    records.EBFINFO.each{
    ebfHistory_info(num:it.EBFNUM.text(),
        release_date_time:it.RELEASEDATETIME.text()
        ){

            it.FILES.FILE.each{ //says Exception in thread "main" java.lang.NullPointerException: Cannot get property 'FILES' on null object
                ebfHistory_fileinfo(file_path:it.PATH.text(),
                    file_checksum:it.CHECKSUM.text()
                ){
                    ebfHistory_fileinfo_affectedfiles(
                        afile_checksum:it.CHECKSUM.text(),
                        afile_path:it.PATH.text()
                        )
                }
            }
        }
  }             
}

如下所示

<ebfHistory>
    <ebfHistory_info num="EBF262323",release_date_time="May 06, 2011">
        <ebfHistory_fileinfo file_checksum="303966974">
            <ebfHistory_fileinfo_affectedfiles afile_checksum="3461283269">
                <path>C:\clients\PmClient\CommandLineUtilities\PC\server\bin\ACE.dll</path>
                <path>C:\clients\PmClient\client\bin\ACE.dll</path>
            </ebfHistory_fileinfo_affectedfiles>
        </ebfHistory_fileinfo>
    </ebfHistory_info>
</ebfHistory>

但我得到Exception in thread "main" java.lang.NullPointerException: Cannot get property 'FILES' on null object我哪里错了?请帮助别人。感谢

更新代码(正常工作)

def records = new XmlParser().parseText(rs)
        csm.ebfHistory(){   
            records.EBFINFO.each{   ebfinfo ->
                ebfHistory_info(num:ebfinfo.EBFNUM.text(),
                    release_date_time:ebfinfo.RELEASEDATETIME.text())
                    {
                        ebfinfo.SYMPTOMS.br.each{
                            ebfHistory_symptom(name:it.text())
                        }
                    }
            }   
            ebfHistory_dump(rs){
                "${rs}"
            }
        }

1 个答案:

答案 0 :(得分:2)

it不再引用每个EBFINFO,因为您处于另一个闭包中 - ebfHistory_info闭包。

相反,明确命名EBFINFO对象:

records.EBFINFO.each { ebfinfo -> // <-- Give it a name
    ebfHistory_info(num:it.EBFNUM.text(),
                    release_date_time:it.RELEASEDATETIME.text()) {
        ebfinfo.FILES.FILE.each { // <-- Use the name here

ebfHistory_fileinfo_affectedfiles参数中的相同内容。