Search code examples
xmlpluginsnsis

Reading root node attribute with Wizous XML plugin in NSIS


I am trying to read a root attribute, but still do not manage to do it using wizous xml.nsh plugin :(

Code itself is straight forward:

  ${xml::LoadFile} "${WHICH_DIR}" $0
  ${xml::RootElement} $R1 $R0
  !ifdef DEBUGMODE_NSISDBG
    nsisdbg::sendtolog /NOUNLOAD " root : $R1"
  !endif
  ${xml::GotoPath} "${XML_PATH}" $R1
  !ifdef DEBUGMODE_NSISDBG
    nsisdbg::sendtolog /NOUNLOAD "GotoPath '${XML_PATH}' result: $R1 "
  !endif
  ${xml::GetAttribute} "${XML_PARAM}" "${XML_VARIABLE}" $R1
  !ifdef DEBUGMODE_NSISDBG
    nsisdbg::sendtolog /NOUNLOAD "GetAttribute '${XML_PARAM}' result '$R1' : ${XML_VARIABLE} "
  ${xml::SetAttribute} "port" "$EditTextEditText" $0
  ${xml::SaveFile} "agent.xml" $0
  ${xml::Unload}

But the output still is not what I need:

<2012.04.17. 13:01:04>  root : agent
<2012.04.17. 13:01:04> GotoPath '/agent/' result: -1 
<2012.04.17. 13:01:04> GetAttribute 'port' result '0' :  

The input XML file:

<?xml version="1.0" encoding="windows-1257"?>
<agent port="0000" loglevel="3">
</agent>

Thank you for any tip or kind of help.


Solution

  • Some preliminary points :

    • as you did not provided a sscc example, I needed to tweak your code a little
    • it seems that you are not using the NsisXml plugin by Wizou but the XML plugin by Instructor

    While working on your sample I noticed that

    • you might be confused between nsis constants and variables (you should look at the warnings that are outputed by the nsis compiler) as GetAttribute expects a variable as 2nd parameter to store the result
    • GotoPath does not work with a final / I have the expected result by removing it
    • I have changed the code as I do not use the nsisdbg plugin but only DebugView to catch OutputDebugString messages
    !include "XML.nsh"
    
    Name "Sample nsisXML"
    OutFile "SampleSO.exe"
    
    ShowInstDetails show    
    
    !define XML_PATH "/agent"
    !define XML_PARAM "port"
    var XML_VARIABLE 
    
    !define DEBUG `System::Call kernel32::OutputDebugString(ts)`
    
    !macro GetXMLParam PATH PARAM VARIABLE
        ${xml::GotoPath} "${PATH}" $R1
        ${DEBUG} "GotoPath '${PATH}' result: $R1 "
        ${xml::GetAttribute} "${PARAM}" ${VARIABLE} $R1
        ${DEBUG} "GetAttribute '${PARAM}' result '$R1' : ${VARIABLE} "
    !macroend
    
    Section "Main program"
    
        ${xml::LoadFile} "so.xml" $0
        ${xml::RootElement} $R1 $R0
        ${DEBUG} " root : $R1"
    
        !insertmacro GetXMLParam "/agent" "port" $XML_VARIABLE
    ;        ${xml::GotoPath} "${XML_PATH}" $R1
    ;         ${DEBUG} "GotoPath '${XML_PATH}' result: $R1 "
    ;         ${xml::GetAttribute} "${XML_PARAM}" $XML_VARIABLE $R1
    ;         ${DEBUG} "GetAttribute '${XML_PARAM}' result '$R1' : $XML_VARIABLE "
        ${xml::SetAttribute} "port" "$XML_VARIABLE" $0
        ${xml::SaveFile} "agent.xml" $0
        ${xml::Unload}
    SectionEnd
    

    Result :

    root : agent
    GotoPath '/agent' result: 0 
    GetAttribute 'port' result '0' : 0000