Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
226 views
in Technique[技术] by (71.8m points)

xml - Groovy, XmlParser, addNode, inserting a array of elements

I have a loop below to create an array of variables on my XML. The issue is that for the first time it is running, it is including all my rows at the first occurrence in my output array. Any comments are welcome. Many thanks!

pqpwp*.appendNode('PostalAcc', values[0])
pqpwp*.appendNode('CloseAmt', values[1])
pqpwp*.appendNode('Rates',)
def rates = ltrData.PqPwp.Rates
def values = ["Under 0          0.10%   0.08%      0.10%","Under 1          0.10%   0.08%      0.10%","Under 2          0.10%   0.08%      0.10%","Under 3          0.10%   0.08%      0.10%"]

for (int i = 2; i<10; i++)
{
    if (values[i])
    {
        rates*.appendNode('Rate',)
        def rate = ltrData.PqPwp.Rates.Rate
        rate*.appendNode('Column1', values[i].substring(0,14))
        rate*.appendNode('Column2', values[i].substring(15,23))
        rate*.appendNode('Column3', values[i].substring(24,31))
        rate*.appendNode('Column4', values[i].substring(32,42))
    }
}

actual xml output

      <ltrData>
        <PqPwp>
          <PostalAcc>910060121</PostalAcc>
          <CloseAmt>514.17</CloseAmt>
          <Rates>
            <Rate>
              <Column1>Under ?1      2</Column1>
              <Column2>   0.10%</Column2>
              <Column3>  0.08%</Column3>
              <Column4>     0.10%</Column4>
              <Column1>?1 plus       3</Column1>
              <Column2>   1.39%</Column2>
              <Column3>  1.11%</Column3>
              <Column4>     1.40%</Column4>
            </Rate>
            <Rate>
              <Column1>?1 plus       3</Column1>
              <Column2>   1.39%</Column2>
              <Column3>  1.11%</Column3>
              <Column4>     1.40%</Column4>
            </Rate>
          </Rates>

xml output desired

      <ltrData>
        <PqPwp>
          <PostalAcc>910060121</PostalAcc>
          <CloseAmt>514.17</CloseAmt>
          <Rates>
            <Rate>
              <Column1>Under ?1      2</Column1>
              <Column2>   0.10%</Column2>
              <Column3>  0.08%</Column3>
              <Column4>     0.10%</Column4>
            </Rate>
            <Rate>
              <Column1>?1 plus       3</Column1>
              <Column2>   1.39%</Column2>
              <Column3>  1.11%</Column3>
              <Column4>     1.40%</Column4>
            </Rate>
          </Rates>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

the fixed code is below, hope someone else needs it :)

        if (values[i] != "")
            {
            def rate =  rates*.appendNode('Rate')
            rate*.appendNode('Column1',values[i].substring(0,14))
            rate*.appendNode('Column2', values[i].substring(15,23))
            rate*.appendNode('Column3', values[i].substring(24,31))
            rate*.appendNode('Column4', values[i].substring(32,42))
            }    
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...