XML Attributes

This is an http response that will always contain a single node. I am having issues incorporating the document examples to read the attributes of the entry.

<?xml version="1.0"?> <interface-response> <Command>SETDNSHOST</Command> <Language>eng</Language> <IP></IP> <ErrCount>0</ErrCount> <ResponseCount>0</ResponseCount> <Done>true</Done> <debug> <![CDATA[]]> </debug> </interface-response>

My last try looks like this and still returns nothing. x is a string containing the above response.

[code]Dim xml As new XmlDocument

System.DebugLog("IP: " + xml.DocumentElement.FirstChild.GetAttribute(“IP”))
System.DebugLog("ErrCount: " + xml.DocumentElement.FirstChild.GetAttribute(“ErrCount”))
System.DebugLog("ResponseCount: " + xml.DocumentElement.FirstChild.GetAttribute(“ResponseCount”))
System.DebugLog("Done: " + xml.DocumentElement.FirstChild.GetAttribute(“Done”))
System.DebugLog("debug: " + xml.DocumentElement.FirstChild.GetAttribute(“debug”))

add a break just after the loadxml line
then with the debugger look inside the xmldocumment, you will easily where the nodes your’re searching for are.

They are not attribute!
Are Nodes and you need the inner text node value.

your code is for an XML like this:

<?xml version="1.0"?>
    <Command IP="some data" ErrCount="some data" ResponseCount ="some data" Done="some data" debug="some data">SETDNSHOST</Command>


This site is a link must have !

Yes, but the author “preference” is a very old approach that leads to over engineered documents.
A more OOP approach is using attributes as atomic properties and node as non atomic properties (array, list, etc)

In any case the original request (really common) can be solved with this extension function:

Public Function nodeText(extends node as XmlNode, subNodeName as String = "") as String
  If subNodeName="" Then
    If node.FirstChild<>Nil And node.FirstChild IsA XmlTextNode Then Return node.FirstChild.Value
    Dim xql As New XmlNodeList
    If xql.Length=1 Then
      Return xql.Item(0).Value
    End If
  End If
End Function

So (as for request example)

System.DebugLog("IP: " + xml.DocumentElement.nodeText("IP"))
System.DebugLog("ErrCount: " + xml.DocumentElement.nodeText("ErrCount"))
System.DebugLog("ResponseCount: " + xml.DocumentElement.nodeText("ResponseCount"))
System.DebugLog("Done: " + xml.DocumentElement.nodeText("Done"))
System.DebugLog("debug: " + xml.DocumentElement.nodeText("debug"))

Antonio, when I implement your solution, I get

[quote]App.nodeText Declaration
The extends modifier cannot be used on a class method
Function nodeText(extends node as XmlNode, subNodeName as String = “”) As String[/quote]

[quote]App.ParseXML, line 4
Type “XmlElement” has no member named “nodeText”
System.DebugLog("IP: " + xml.DocumentElement.nodeText(“IP”))[/quote]

[quote]App.ParseXML, line 4
Type mismatch error. Expected TextLiteral, but got Int32
System.DebugLog("IP: " + xml.DocumentElement.nodeText(“IP”))[/quote]

[quote]App.ParseXML, line 4
Parameter “msg” expects type String, but this is type No Type.
System.DebugLog("IP: " + xml.DocumentElement.nodeText(“IP”))[/quote]

I have also noticed that the .Value of each node is blank.

I solved it by

[code]Dim xml As new XmlDocument
xml.LoadXml(“<?xml version=""1.0""?>SETDNSHOSTeng10.10.10.1000true”)

Dim n As XmlNode = xml.DocumentElement.FirstChild
Dim d As new Xojo.Core.Dictionary

While n <> Nil
if n.FirstChild <> nil then
System.DebugLog(n.Name + ": " + n.FirstChild.Value)
d.Value(n.Name) = n.FirstChild.Value
end if

n = n.NextSibling

Return d[/code]

Scott, all the methods where the first parameter begins with extends must be placed in a module and must be public

Then you have a new method for the object. In this case you have a new method for the XMLNode called nodeText that (as for getAttribute) reply with the text node contents if exists otherwise with an empty string.

Sorry to have not remarked this in the sample code. (I simply noted that it is an extension code)