Old 15th November 2021, 00:45   #1
pocz
Junior Member
 
Join Date: Nov 2021
Posts: 8
Edit attributes in an XML with nsisXML by Wizou

I can't figure it out.
I try to modify an XML, but I get lost when finding the issue why it's not working.
I have a file "foo.xml"
HTML Code:
<global>
    <colors
      lava="orange"
      sky="blue"
    >
    </colors>
</global>
And I try to achieve this:
HTML Code:
<global>
    <colors
      lava="orange"
      sky="cyan"
      earth="brown"
    >
    </colors>
</global>
Let's assume I am not sure if the attributes sky and earth exist in the file, before editing.
This is how my NSIS code looks like:

code:
nsisXML::create
nsisXML::load "foo.xml"
nsisXML::select "/global/colors"
nsisXML::setAttribute "sky" "cyan"
nsisXML::setAttribute "earth" "brown"
nsisXML::save



I haven't found any information in the documentation how to overwrite, add, or remove attributes with their values. Do I have to remove entire nodes, and append them again in the end?
pocz is offline   Reply With Quote
Old 15th November 2021, 01:25   #2
pocz
Junior Member
 
Join Date: Nov 2021
Posts: 8
I think nsisXML by Joel will be the easier solution to my problem.
code:
${nsisXML->removeAttr}
${nsisXML->SetElementAttr}



The simpliest way would be just to add the attributes, and hoping the old ones will be overwritten.
But knowing life, I probably have to check the attribute to see if it exists, then eventually remove it, and then add new attributes:
(Check if "sky" and "earth" exist, if they exist - remove them, create new attributes "sky" and "earth" with new values)
pocz is offline   Reply With Quote
Old 15th November 2021, 10:49   #3
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,450
The Wizou plugin uses MSXML and MSDN says: "Sets or updates the supplied attribute node on this element"

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 15th November 2021, 13:42   #4
pocz
Junior Member
 
Join Date: Nov 2021
Posts: 8
I got it.
This in nsisXML by Wizou should be written in an other way, so it would work.
code:
nsisXML::create
nsisXML::load "foo.xml"
nsisXML::select "/global/colors"
nsisXML::setAttribute "sky" "cyan"
nsisXML::setAttribute "earth" "brown"
nsisXML::save


But it didn't work for me.
But this below did work, in nsisXML by Joel. Last time updated in 2009.
code:
${nsisXML->OpenXML} "foo.xml"
${nsisXML->SetElementAttr} "/global/colors" "sky" "cyan"
${nsisXML->SetElementAttr} "/global/colors" "earth" "brown"
${nsisXML->Release} "foo.xml"


I didn't have to remove anything. There are no clones of attributes, it just works, and looks simple.

If you have any ideas how the Wizous code should look like, please post your example.
pocz is offline   Reply With Quote
Old 15th November 2021, 18:59   #5
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,450
Quote:
Originally Posted by pocz View Post
If you have any ideas how the Wizous code should look like, please post your example.
PHP Code:
FileOpen $0 "$temp\test.xml" w
FileWrite $0 '<?xml version="1.0" encoding="ISO-8859-1" ?><global><colors lava="orange" sky="blue"></colors></global>'
FileClose $0

nsisXML::create
DetailPrint $0,$1
nsisXML::load  "$temp\test.xml"
DetailPrint $0,$1
nsisXML::select "/global/colors"
nsisXML::setAttribute "sky" "cyan"
nsisXML::setAttribute "earth" "brown"
strcmp $0 0 +3
nsisXML::save "$temp\test.xml"
nsisXML::release $0

nsexec::ExecToLog 'cmd.exe /C type "$temp\test.xml"'
Pop $0

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 15th November 2021, 19:58   #6
pocz
Junior Member
 
Join Date: Nov 2021
Posts: 8
Ah, yes. I forgot to save the file properly.
nsisXML::save requires an argument.

The downside of the two nsisXML versions is,
that the structure is being rebuilt.
This is the result I was trying to achieve:
HTML Code:
<global>
    <colors
      lava="orange"
      sky="cyan"
      earth="brown"
    >
    </colors>
</global>
But instead I get this:
HTML Code:
<global>
    <colors lava="orange" sky="cyan" earth="brown">
    </colors>
</global>
The problem is, that the XML file has about a hundred of attributes in a single node, and this many attributes in a single line is a bit messy. Anyway, at least other programs can read all the values properly.
Attached Files
File Type: zip Foo.zip (700 Bytes, 99 views)
pocz is offline   Reply With Quote
Old 15th November 2021, 21:58   #7
pocz
Junior Member
 
Join Date: Nov 2021
Posts: 8
Quote:
The problem is, that the XML file has about a hundred of attributes in a single node, and this many attributes in a single line is a bit messy.
Instead in an XML file, the dozens of attributes should have been stored in an INI file. It would be easier to read. ..
pocz is offline   Reply With Quote
Old 15th November 2021, 22:02   #8
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,450
Quote:
Originally Posted by pocz View Post
The downside of the two nsisXML versions is,
that the structure is being rebuilt.
This will only save some of the whitespace but it is the best you are going to get with MSXML I'm guessing

PHP Code:
FileOpen $0 "$temp\test.xml" w
FileWrite $0 '<?xml version="1.0" encoding="ISO-8859-1" ?>$\r$\n$\r$\n<global><colors lava="orange" sky="blue">$\r$\n$\r$\n</colors>$\r$\n$\r$\n</global>'
FileClose $0

nsisXML::create
DetailPrint $0,$1
strcmp $0 0 +2
System::Call `$0->72(i-1)` ; put_preserveWhiteSpace
nsisXML::load  "$temp\test.xml"
nsisXML::select "/global/colors"
nsisXML::setAttribute "sky" "cyan"
nsisXML::setAttribute "earth" "brown"
strcmp $0 0 +3
nsisXML::save "$temp\test.xml"
nsisXML::release $0

nsexec::ExecToLog 'cmd.exe /C type "$temp\test.xml"'
Pop $0

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Developer Center > NSIS Discussion

Tags
attribute, joel, wizou, xml

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump