1

我有一个 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 是动态的并且大小(标签)可以增加所以我认为将数据存储到结构数组中(不单独数组)将是更好的方法.....

4

2 回答 2

1

NSIS 没有对数组的本机支持,但您可以使用此处列出的备选方案之一。

于 2012-07-26T23:20:06.957 回答
1

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

然后使用一些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 添加valueat key,用该键替换任何现有值。

于 2012-07-27T04:37:12.250 回答