简体   繁体   English

如果使用 NSIS 安装程序在 xml 中为空,则无法更新特定元素的值

[英]Unable to update value of particular element if it is empty in xml using NSIS installer

I am working on NSIS [Nullsoft Installer System] Installer script.我正在研究 NSIS [Nullsoft 安装程序系统] 安装程序脚本。 I am facing issue to update XML element value [used XML plugin] when element is empty for example,例如,当元素为空时,我面临更新 XML 元素值 [使用的 XML 插件] 的问题,

**Element value to be updated: <Name />
After executing the below script, the output is : <Test />**

${xml::LoadFile} "$EXEDIR\install1.config" $0
${If} $0 != -1
  ${xml::GotoPath} "/InstallerInputs/Name" $0
  ${xml::FirstChild} "" $0 $1
  ${xml::SetNodeValue} "Test"
  ${xml::SaveFile} "$EXEDIR\install1.config" $0
  ${xml::Unload}
  ${EndIf}

XML structure is like below, XML结构如下,

<?xml version="1.0" encoding="utf-16" ?>
<InstallerInputs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   **<Name></Name>** or **<Name />**
   <Password></Password>
</InstallerInputs>

Output XML:输出 XML:

<?xml version="1.0" encoding="utf-16" ?>
<InstallerInputs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   **<Test/>**
   <Password></Password>
</InstallerInputs>

Any help....任何帮助....

Two issues:两个问题:

  1. After GotoPath you are already at the correct node, ${xml::FirstChild} will fail because Name has no children.在 GotoPath 之后,您已经在正确的节点上, ${xml::FirstChild}将失败,因为 Name 没有子节点。
  2. ${xml::SetNodeValue} seems to set the tag name, not the inner text. ${xml::SetNodeValue}似乎设置标签名称,而不是内部文本。

Try this尝试这个

!if 0
FileOpen $0 "$Temp\test.xml" w
FileWrite $0 '<?xml version="1.0" encoding="utf-16" ?>$\r$\n'
FileWrite $0 '<InstallerInputs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">$\r$\n'
FileWrite $0 '<Name></Name>$\r$\n'
FileWrite $0 '<Password></Password>$\r$\n'
FileWrite $0 '</InstallerInputs>$\r$\n'
FileClose $0
!endif

${xml::LoadFile} "$Temp\test.xml" $0
${If} $0 != -1
    ${xml::GotoPath} "/InstallerInputs/Name" $0
    ${If} $0 = 0
        ${xml::SetText}  "Test" $0
        ${xml::SaveFile} "$Temp\test.xml" $0
    ${EndIf}
    ${xml::Unload}
${EndIf}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM