Announcement

Collapse
No announcement yet.

XML Manipulation

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • XML Manipulation

    Hi

    I'm having trouble determining what the XML path should be to access a specific part of an XML file.


    Here is the XML....

    <?xml version="1.0"?>
    <configuration>
    <appSettings file="">
    <clear />
    <add key="SettingOne" value="A" />
    <add key="SettingTwo" value="B" />
    <add key="SettingThree" value="C" />
    <add key="SettingFour" value="D" />
    <add key="SettingFive" value="E" />
    </appSettings>
    </configuration>



    Here is the code I'm using to test the XML path...

    Code:
    local function XMLTest(strPath)
        
        local strSource = _TempLaunchFolder.."\\test.XML"    
        local strElementXML 
        local strValueXML
        Debug.Print("Loading XML: "..strSource.."\r\n")
        
        XML.Load(strSource)
        
        
        Debug.Print("Attempting to get path: "..strPath.."\r\n")
        
        strElementXML = XML.GetElementXML(strPath)
        error = Application.GetLastError();
        if (error ~= 0) then                                                                                
            strElementXML = _tblErrorMessages[error]                            
        end 
        
        strValueXML = XML.GetValue(strPath)
        error = Application.GetLastError();
        if (error ~= 0) then                                                                                
            strValueXML = _tblErrorMessages[error]                            
        end 
        
        Debug.Print("Element XML: "..strElementXML.."\r\n")
        Debug.Print("Value XML: "..strValueXML.."\r\n")
        
        --
    end
    What would the XML path be if I'm trying to extract the values "SettingThree" and "C"?
    I have tried configuration/appSettings/add:3/key but this causes an error: "The specified XML path is not valid or was not found."
    When I use the path configuration/appSettings/add:3 GetElementXML does return the complete line, e.g. <add key="SettingThree" value="C" /> but I need the individual values within that.

    Thanks in advance




  • #2
    What would the XML path be if I'm trying to extract the values "SettingThree" and "C"?
    I have tried configuration/appSettings/add:3/key but this causes an error: "The specified XML path is not valid or was not found."
    When I use the path configuration/appSettings/add:3 GetElementXML does return the complete line, e.g. <add key="SettingThree" value="C" /> but I need the individual values within that
    The issue here is that these are not values, like you wrote. These are attributes. Please see the XML Concepts chapter in the help file, where it is explained in better detail.

    Code:
    <phone location="daytime">(204)946-0244</phone>
    (204)946-0244 is a value, but daytime is an attribute. In your code, you cannot use XML.GetValue() to retrieve an attribute.

    Here is some modified and working code:

    Code:
    local function XMLTest(strPath, strAttrib)
        
        local strSource = _TempLaunchFolder.."\\test.XML";
        Debug.Print("Loading XML: "..strSource.."\r\n");
        
        XML.Load(strSource);
        
        Debug.Print("Attempting to get '" .. strAttrib .. "' in  '" .. strPath .. "'\r\n");
        
        local strAttributeXML = XML.GetAttribute(strPath, strAttrib);
        error = Application.GetLastError();
        if (error ~= 0) then                                                                                
            Debug.Print(_tblErrorMessages[error] .. "\r\n");
        else
            Debug.Print("Attribute XML: "..strAttributeXML.."\r\n")
        end
    
    end
    
    Debug.ShowWindow();
    XMLTest("configuration/appSettings/add:3", "key");
    And this produces this output:

    Code:
    Loading XML: C:\Users\ULRICH~1\AppData\Local\Temp\_ir_sf_temp_0\test.XML
    Attempting to get 'key' in  'configuration/appSettings/add:3'
    Attribute XML: SettingThree
    Ulrich
    Last edited by Ulrich; 10-19-2017, 05:42 PM. Reason: typo

    Comment


    • #3
      This helped a lot, many thanks.

      Ben

      Comment

      Working...
      X