如何在NSIS中创建结构数组

时间:2012-07-26 05:09:33

标签: nsis

我有一个XML文件....现在我想根据我在NSIS SCRIPT中的要求解析它并将数据存储到结构数组中。

  This is my XML:-

  <?xml version="1.0" ?> 
- <request>
- <software>
  <id>1</id> 
  <name>software</name> 
  <description>is a file archiver with a high compression ratio.</description> 
  <download_url>http://surfnet.dl.sourceforge.net/project/sevenzip/7-Zip/9.20/7z920.exe</download_url> 
  <image_url>http://assets.airinstaller.com/graphics/software/7zip/left_side.bmp</image_url> 
  </software>
- <offers>
- <offer>
  <rank>1</rank> 
- <options>
- <option>
 <text>Install Facemoods1</text> 
  <value>/TOOLBAR</value> 
  <required>true</required> 
  </option>
- <option>
  <text>Set Facemoods as my default search provider1</text> 
  <value>/DEFAULTSEARCH</value> 
  <required>false</required> 
  </option>
  </options>
  <install_location>{pf}/IssPro c.dll</install_location> 
   <name>1</name> 
  <tagline>The Ringtone Maker</tagline> 
  <description>makes creating ringtones really easy.</description> 
  <exe_cmd /> 
  <download_url>http://174.36.153.163/silent-ringtonejunkiez-installer.exe</download_url> 
  <conversion_url>http://api.airinstaller.com/get/installed.php?session=9649922a0d879d92752f3925e07153c3&app=2&aff=1&off=1</conversion_url> 
     <image_url>http://assets.airinstaller.com/graphics/offers/ringtonejunkiez/logo.bmp</image_url> 
  <privacy_url>http://www.google.com/intl/en/privacy/privacy-policy.html</privacy_url> 
  <terms_url>http://www.google.com/accounts/TOS</terms_url> 
  </offer>
- <offer>
  <rank>2</rank> 
- <options>
- <option>
  <text>Install Facemoods</text> 
  <value>/TOOLBAR</value> 
  <required>true</required> 
  </option>
- <option>
  <text>Set Facemoods as my default search provider</text> 
  <value>/DEFAULTSEARCH</value> 
  <required>false</required> 
  </option>
- <option>
  <text>Set Facemoods as my default home and new tab page</text> 
  <value>/DEFAULTSTART</value> 
  <required>false</required> 
  </option>
  </options>
  <name>Ringtone 2</name> 
  <tagline>The Ringtone Maker</tagline> 
  <description>makes creating ringtones really easy.</description> 
   <exe_cmd /> 
  <download_url>http://174.36.153.163/silent-ringtonejunkiez-installer.exe</download_url> 
  <conversion_url>http://api.airinstaller.com/get/installed.php?session=9649922a0d879d92752f3925e07153c3&app=2&aff=1&off=1</conversion_url> 
   <image_url>http://assets.airinstaller.com/graphics/offers/ringtonejunkiez/logo.bmp</image_url> 
   <privacy_url>http://www.google.com/intl/en/privacy/privacy-policy.html</privacy_url> 
   <terms_url>http://www.google.com/accounts/TOS</terms_url> 
  </offer>
- <offer>
  <rank>3</rank> 
  <id>13</id> 
  <install_location>{pf}/Real/RealPlayer/f ixrjb.exe</install_location> 
  <name>3</name> 
  <description>makes creating ringtones really easy.</description> 
  <exe_cmd /> 
  <download_url>http://174.36.153.163/silent-ringtonejunkiez-installer.exe</download_url> 
  <conversion_url>http://api.airinstaller.com/get/installed.php?session=9649922a0d879d92752f3925e07153c3&app=2&aff=1&off=1</conversion_url> 
   <image_url>http://assets.airinstaller.com/graphics/offers/ringtonejunkiez/logo.bmp</image_url> 
   <privacy_url>http://www.google.com/intl/en/privacy/privacy-policy.html</privacy_url> 
   <terms_url>http://www.google.com/accounts/TOS</terms_url> 
   </offer>
   </offers>
   </request>

请任何人帮我解析并存储到NSIS脚本中的结构数组......

我被困在这里......

感谢您的回复,但我没有得到这个.......
我用这个方法解析了XML

${XML::GotoPath} "/request/software" $0 

        ${XML::NextSiblingElement} "" $0 $1
                        DetailPrint "Next Sibling Element of Root Element=$0"          ; SIBLING OF SOFTWARE (SECOND CHILD OF ROOT)


           ${XML::XPathString} "count(//offer)" $0 $1
           StrCpy $ccount_offer $0
             ;${If} $0 != ""
           ; IntOp $count_offer $count_offer + 1
            ; DetailPrint "Count= $0 and $count_offer"
           ;Goto for
            ;${Else}
           ;Goto next
           ;${EndIf}


   ${For} $R1 1 $ccount_offer
         ${If} $R1 == 1
           ${XML::FirstChildElement} "" $0 $1
                 DetailPrint "Element in Offers=$0"                                ; FIRST ElEMENT    IN OFFERS
           ${xml::ElementPath} $0
     StrCpy $ccurrent_path $0
         ${Else}
           ${XML::NextSiblingElement} "" $0 $1
           DetailPrint "Element in offer=$0"
             ${xml::ElementPath} $0
     StrCpy $ccurrent_path $0
                ${EndIf}

          ${XML::XPathString} "count(child::*)" $0 $1
          ;DetailPrint "count======$0"
     StrCpy $ccount_offer_child  $0
       ${XML::FirstChildElement} "" $0 $1                       ;first child of offer
      DetailPrint "Element in Offer=$0"
          ${XML::GetText} $0 $1
     DetailPrint "Text in it=$0"
       IntOp $ccount_offer_child $ccount_offer_child - 1
  ${For} $R2 1 $ccount_offer_child

            ;${If} $R2 == 1


       ;${Else}

          ${XML::NextSiblingElement} "" $0 $1
     DetailPrint "Element in Offer=$0"


           ${If} $0 == "options"                                  ; options for loops


             ${xml::ElementPath} $0
       StrCpy $llast_current_path $0

          ${XML::XPathString} "count(child::*)" $0 $1
          StrCpy $ccount_options  $0

    ${If} $0 != 0

    ${For} $R3 1 $ccount_options
         ${If} $R3 == 1
          ${XML::FirstChildElement} "" $0 $1
        DetailPrint "Element in Options=$0"

          ${xml::ElementPath} $0
          StrCpy $llast_last_current_path $0


          ${XML::XPathString} "count(child::*)" $0 $1
          StrCpy $ccount_options_option  $0

      ${For} $R4 1 $ccount_options_option
            ${If} $R4 == 1

         ${XML::FirstChildElement} "" $0 $1
     DetailPrint "Element in Option=$0"

  ${XML::GetText} $0 $1
          DetailPrint "Text in it=$0"
          ${Else}

         ${XML::NextSiblingElement} "" $0 $1
     DetailPrint "Element in Option=$0"

 ${XML::GetText} $0 $1
         DetailPrint "Text in it=$0"

${EndIf}


    ${Next}



         ${XML::GotoPath} $llast_last_current_path $0

${Else}

      ${XML::GotoPath} $llast_last_current_path $0

        ${XML::NextSiblingElement} "" $0 $1
      DetailPrint "Element in Options=$0"

          ${XML::XPathString} "count(child::*)" $0 $1
         StrCpy $ccount_options_option  $0

  ${For} $R4 1 $ccount_options_option
          ${If} $R4 == 1

        ${XML::FirstChildElement} "" $0 $1
      DetailPrint "Element in Option=$0"

  ${XML::GetText} $0 $1
         DetailPrint "Text in it=$0"

           ${Else}

        ${XML::NextSiblingElement} "" $0 $1
     DetailPrint "Element in Option=$0"

 ${XML::GetText} $0 $1
         DetailPrint "Text in it=$0"

    ${EndIf}

${Next}

         ${EndIf}




${Next}

   ${EndIf}
                 ${XML::GotoPath} $llast_current_path $0          ;end of options      for loops


     ${ElseIf} $0 == "exe_cmd"
     ${Else}
     ${XML::GetText} $0 $1
     DetailPrint "Text in it=$0"
         ${EndIf}

        ; ${EndIf}

${Next}

        ${XML::GotoPath} "$ccurrent_path" $0

${Next}                                               

现在我希望将数据存储到结构数组中而不是简单的数组.......因为XML是动态的,并且可以增加大小(标签)所以我认为将数据存储到结构数组中(不是单独的阵列)将是更好的aproach .....

2 个答案:

答案 0 :(得分:1)

NSIS没有对数组的本机支持,但您可以使用here列出的替代方案之一。

答案 1 :(得分:1)

首先,您需要使用一些XML插件解析XML中的数据(例如:http://nsis.sourceforge.net/XML_plug-in)。

然后使用一些http://nsis.sourceforge.net/Arrays_in_NSIS插件将数据保存在数组中(建议使用nsArray插件)。

你想将整个树保存在数组中吗?我认为这是一项非常艰巨的任务,因为有很多可能性:你想保存所有元素还是只保存非空元素? 或者你只是想读一些值?

请更具体地说明数组的外观。

编辑:(示例)

只需在循环中遍历已加载的树并保存数据:

  IntOp $R1 0 + 0 ; Set $R1 to zero at beginning

  ${While} End_Of_XML_Tree

    ; Fill $your_value with the data you want (element name, value or even whole node path)

    nsArray::Set your_array /key=$R1 $your_value ; Save value to your_array at key = $R1
    IntOp $R1 $R1 + 1 ; Increase $R1 (next key)
  ${EndWhile}

nsArray :: Set在value添加key,用该键替换任何现有值。