WasStaX SetNodeValue

From Second Life Wiki
Jump to: navigation, search

SetNodeValue

This function traverses a flat XML file, passes as the parameter xmlStream and sets the data contained in the node, node and returns the modified xmlStream.

For further details, please see Wizardry and Steamworks/StaX.

<lsl> ///////////////////////////////////////////////////////// // [K] Kira Komarov - 2011, License: GPLv3 // //////////////////////////////////////////////////////////

string wasStaX_SetNodeValue(string xmlStream, string node, string value) {

   list stream = llParseString2List(xmlStream, [""], ["<", ">", "/"]);
   integer size = llGetListLength(stream);
   list StaX = [];
   integer ptr = 0;
   integer set = 0;
   do {
       string current = llList2String(stream, ptr);
       string lookback = llList2String(stream, ptr-1);
       if(current != "/" && lookback == "<") {
           StaX += current;
           jump next_tag;
       }
       if(lookback == "/") {
           StaX = llDeleteSubList(StaX, llGetListLength(StaX)-1, llGetListLength(StaX)-1);
           jump next_tag;
       }
       if(current != ">" && current != "/" && current != "<") 
           if(llList2String(StaX,llGetListLength(StaX)-1) == node) {
               if(!set) {
                   stream = llListReplaceList(stream, (list)value, ptr, ptr);
                   set = 1;
                   jump next_tag;
               }
               stream = llListReplaceList(stream, (list)"", ptr, ptr);
           }

@next_tag;

   } while(++ptr<size);
   
   if(llGetListLength(StaX) != 0) {
       llSay(DEBUG_CHANNEL, "The following tags may be unmatched: " + llDumpList2String(StaX, ",") + ". Please check your file.");
   }
   return llDumpList2String(stream, "");

} </lsl>