revSetXMLAttribute

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
selvabits
Posts: 13
Joined: Tue Jul 17, 2007 4:30 pm

revSetXMLAttribute

Post by selvabits » Thu Dec 06, 2007 1:25 am

Hi , How to change the attribute of a node by using the value of another attribute of the nodes

Thanks

malte
Posts: 1098
Joined: Thu Feb 23, 2006 8:34 pm
Location: Ostenfeld germany
Contact:

Post by malte » Thu Dec 06, 2007 1:51 am

Hi,

this should work:

local tNewValue
put revXmlAttribute(tId,"/path/to/node","attributename") into tNewValue
revSetXMLAttribute tId,"/path/to/node","otherAttribute",tNewValue

the names for the commands and functions is a bit inconsistant and takes getting used to. Best search for XML in the dictionary.

Hope that helps,

Malte

selvabits
Posts: 13
Joined: Tue Jul 17, 2007 4:30 pm

Post by selvabits » Thu Dec 06, 2007 5:05 pm

The problem is that i dont know how to give the nodepath
because my XML structure is like this
<EdgeType id="1" name="ReportsTo" visible="true"></EdgeType>
<EdgeType id="2" name="HasInfluenceOn" visible="true"></EdgeType>

So can you tell me how to give the nodepath if it is like this , I want to change the visible attribute based on the id

Thanks
Bye

Twit
Posts: 5
Joined: Sun Dec 02, 2007 6:39 am

Post by Twit » Thu Dec 06, 2007 10:12 pm

Does that revSetXMLAttribute method take a proper xpath in the second parameter? If so, given what malte wrote above then it should be something along the lines of:

revSetXMLAttribute tId, "//EdgeType[@id='1']", "visible", tNewValue

...but I'm a complete noob at runrev so I may be way off.

selvabits
Posts: 13
Joined: Tue Jul 17, 2007 4:30 pm

Post by selvabits » Thu Dec 06, 2007 11:57 pm

Thanks a lot , that works , but one more doubt

put url ("file:" & "In.xml") into myFileData
put revCreateXMLTree(myFileData, false, true, false) into tDocID
put "/Graph/EdgeTypes/EdgeType[@id='1']" into nodepath
put "false"into tnew
revSetXMLAttribute tDocID,nodepath,"visible",tnew

This way i load In.xml make the changes , how do i save it back to the .xml , this may be very trivial but i am new to runtime revolution

Thanks

malte
Posts: 1098
Joined: Thu Feb 23, 2006 8:34 pm
Location: Ostenfeld germany
Contact:

Post by malte » Fri Dec 07, 2007 12:10 am

Hi,

you might want to look at revXmlChildNames. you can reference to a node given a number in brackets then

revXmlNodeContents(tId,"/path/to/node[1]")

If you want to write the DOM object back to the XML file you will want to look at

revXMLText(tID)

for example put
revXmlText(tid) into URL ("file:"&"path/to/file.xml")

The XML stuff in rev is quite powerful, however it requires some reading. I really suggest you make yourself a HUGE can of coffee and filter the dictionary wit XML. Quite a lot to read and play with.

All the best,

Malte

selvabits
Posts: 13
Joined: Tue Jul 17, 2007 4:30 pm

Post by selvabits » Fri Dec 07, 2007 12:33 am

put url ("file:" & "In.xml") into myFileData
put revCreateXMLTree(myFileData, false, true, false) into tDocID
put "/Graph/EdgeTypes/EdgeType[@id='1']" into nodepath
put revXMLNodeContents(tDocID,nodepath) into initcontents
put "false"into tnew
revSetXMLAttribute tDocID,nodepath,"visible",tnew
put revXMLNodeContents(tDocID,nodepath) into contents
put revXMLText(tDocID) into URL "file:In.xml"

This does not work , moreover when i tried to see the value of initcontents it showed xmlerr, can find element
I also tried giving
revXMLNodeContents(tDocID,"/Graph/EdgeTypes/EdgeType[1]" )
What STUPID mistake am i making

Thanks

malte
Posts: 1098
Joined: Thu Feb 23, 2006 8:34 pm
Location: Ostenfeld germany
Contact:

Post by malte » Fri Dec 07, 2007 1:04 am

Nothing stupid I think :) It is just complex to grook the Rev commands.

First of all when creating your tree you may want to do it like this

put revCreateXMLTree(myFileData, true true, false) into tDocID

There was a bug in the docs up to 2.8.1 that will parse even faulty XML. the param there should read don't parse bad data instead of parse bad data.


Secondly to get the path to a node while referencing an attribute you can use
revXmlMatchingNode function

example: I have a field with the following contents:

<test>
<EdgeType id="1" name="ReportsTo" visible="true"></EdgeType>
<EdgeType id="2" name="HasInfluenceOn" visible="true"></EdgeType>
</test>

and a button with this code:

Code: Select all

on mouseUp pMouseBtnNo
    local tDocId
    put revCreateXmlTree(the text of fld 1,true,true,false) into tDocId
    put revXmlMatchingNode(tDocId,revXmlRootNode(tDocId),"EdgeType","id","1",1)
    revDeleteXmlTree tDocId
end mouseUp
Which puts this in the messagebox:

/test/EdgeType[1]

Hope that helps.

Malte

Twit
Posts: 5
Joined: Sun Dec 02, 2007 6:39 am

Post by Twit » Fri Dec 07, 2007 1:45 am

I'm curious about this because I'll probably want to do something similar in the future. Sorry selvabits if I'm getting in your way here, but wouldn't the following do the task of loading some XML, changing an attribute, and then saving it back again? (some variables removed for simplicity)

put url ("file:In.xml") into myFileData
put revCreateXmlTree(myFileData, true, true, false) into tDocId
revSetXmlAttribute(tDocId, "//Graph/EdgeTypes/EdgeType[@id='1']", "visible", false)
put revXmlText(tDocId) into URL "file:In.xml"
revDeleteXmlTree tDocId

I'm assuming that revCreateXmlTree does the equivalent of creating a reference to an XmlDocument, and revSetXmlAttribute works on the instance in the reference provided and takes an xpath. The only reason I could think of for that not working is if revSetXmlAttribute does not work with xpath, and so you are forced to use revXmlMatchingNode. That would be extremely annoying, as you would end up with something like:

put url ("file:In.xml") into myFileData
put revCreateXmlTree(myFileData, true, true, false) into tDocId
put revXmlMatchingNode(tDocId,revXmlRootNode(tDocId),"EdgeType","id","1",1) into tPath
revSetXmlAttribute(tDocId, tPath, "visible", false)
put revXmlText(tDocId) into URL "file:In.xml"
revDeleteXmlTree tDocId

Again, sorry selvabits if this is off track. I'll be interested to see your final outcome.

selvabits
Posts: 13
Joined: Tue Jul 17, 2007 4:30 pm

Post by selvabits » Fri Dec 07, 2007 2:17 am

Hello Twit and Melto

Thanks a lot for your replies

when i give this in the message box put revXmlText(22) 22 being the tDocId
I still see the change not reflected ,i.e , it still remains the same , visible has not been changed to false
revSetXMLAttribute is not working in this case
What could possibly be wrong

Thanks a lot again

malte
Posts: 1098
Joined: Thu Feb 23, 2006 8:34 pm
Location: Ostenfeld germany
Contact:

Post by malte » Fri Dec 07, 2007 11:06 am

Hi Twit,

I can not get the @ thing to work. matching node is the way to go I am afraid.

All the best,

Malte

selvabits
Posts: 13
Joined: Tue Jul 17, 2007 4:30 pm

Post by selvabits » Fri Dec 07, 2007 5:46 pm

Hi

RevSetXMLAttribute doesnt work for me , it says handler cant be found
Should i include something to make it work
Sorry for the trouble
Thanks

malte
Posts: 1098
Joined: Thu Feb 23, 2006 8:34 pm
Location: Ostenfeld germany
Contact:

Post by malte » Fri Dec 07, 2007 7:09 pm

could you please post your complete script?

All the best,

Malte

selvabits
Posts: 13
Joined: Tue Jul 17, 2007 4:30 pm

Post by selvabits » Fri Dec 07, 2007 7:10 pm

Thanks a lot guys it worked , but how do i find the current folders location using runtime revolution

Thanks
Bye

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Location: Aalst, Belgium
Contact:

Post by Janschenkel » Mon Dec 10, 2007 1:30 pm

Check out 'the defaultFolder' in the Rev dictionary - this should give you the information you want, and you can both get and set it.
When your standalone applicaiton is opened, this will point to the folder containing the standalone.app package. On Windows, this is where the executable resides, but on MacOSX, your executable will be two levels deeper within the .app package inside ./Contents/MacOS/

Hope this helped,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

Post Reply

Return to “Talking LiveCode”