Example: StAX Writer

Input notecard

<xml>
    <about>
        <description>
                            This is a StAX XML stream parser, 
                            with look-back and suitable for streams.</description>
   </about>
   <usage>
 
    wasStAX_GetNodeValue(string xmlStream, string node)
 
    where xmlStream is a a flat string containing the xml file and,
    node is a node containing the value you wish to extract.
 
   </usage>
</xml>

Output

Object: It's awesum! Very awezum! 
Object: DUMP: <xml>    <about>        <description>It's awesum! Very awezum!</description>   </about>   <usage>    wasStAX_GetNodeValue(string xmlStream, string node)        where xmlStream is a a flat string containing the xml file and,    node is a node containing the value you wish to extract.   </usage></xml>
Object: wasStAX_GetNodeValue(string xmlStream, string node) where xmlStream is a a flat string containing the xml file and, node is a node containing the value you wish to extract. 

Code: SetNodeValue Example

stax_setnodevalueexample.lsl
///////////////////////////////////////////////////////////////////////////
//  Copyright (C) Wizardry and Steamworks 2011 - License: GNU GPLv3      //
//  Please see: http://www.gnu.org/licenses/gpl.html for legal details,  //
//  rights of fair usage, the disclaimer and warranty conditions.        //
///////////////////////////////////////////////////////////////////////////
string wasStAX_GetNodeValue(string xmlStream, string node) {
    list stream = llParseString2List(xmlStream, [" "], ["<", ">", "/"]);
    integer size = llGetListLength(stream);
    list StAX = [];
    string value = "";
    integer ptr = 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, -1, -1);
            jump next_tag;
        }
        if(current != ">" && current != "/" && current != "<") 
            if(llList2String(StAX,llGetListLength(StAX)-1) == node)
                value += current + " ";  
@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 value;
}
 
///////////////////////////////////////////////////////////////////////////
//  Copyright (C) Wizardry and Steamworks 2011 - License: GNU GPLv3      //
//  Please see: http://www.gnu.org/licenses/gpl.html for legal details,  //
//  rights of fair usage, the disclaimer and warranty conditions.        //
///////////////////////////////////////////////////////////////////////////
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, "");
}
 
key nQuery = NULL_KEY;
integer xLine = 0;
list xList = [];
//pragma inline
string xName = "Input";
 
string sCnt = "";
 
default {
    state_entry() {
        if(llGetInventoryType(xName) != INVENTORY_NOTECARD) {
            llOwnerSay("Failed to find notecard.");
            return;
        }
        nQuery = llGetNotecardLine(xName, xLine);
    }
 
    dataserver(key id, string data) {
        if(id != nQuery) return;
        if(data == EOF) {
            sCnt = wasStAX_SetNodeValue(sCnt, "description", "It's awesum! Very awezum!");
            llOwnerSay(wasStAX_GetNodeValue(sCnt, "description"));
            llOwnerSay("DUMP: " + sCnt);
            llOwnerSay(wasStAX_GetNodeValue(sCnt, "usage"));
            return;
        }
        if(data == "") jump next_line;
        sCnt += data;
@next_line;
        nQuery = llGetNotecardLine(xName, ++xLine);
    }
    changed(integer change) {
        if(change & CHANGED_INVENTORY) llResetScript();
    }
}

secondlife/stax/examples/notecard_writer.txt ยท Last modified: 2022/11/24 07:45 by 127.0.0.1

Access website using Tor Access website using i2p Wizardry and Steamworks PGP Key


For the contact, copyright, license, warranty and privacy terms for the usage of this website please see the contact, license, privacy, copyright.