Sample code for 30+ languages & platforms
Lua

Methods for Getting Attributes

See more XML Examples

Demonstrates some methods for getting attribute name/values.

The input XML, available at http://www.chilkatsoft.com/data/car.xml, is this:

<root>
    <car color="black" make="mercedes" model="C350" hp="302" engine="v6" type="sedan">Mercedes Benz C350</car>
</root>

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

local xml = chilkat.Xml.new()

-- The sample input XML is available at http://www.chilkatsoft.com/data/car.xml
local success = xml:load_xml_file("car.xml")
if not success then
    print(xml.last_error_text)
    return
end

-- Navigate to the "car" node, which is the 1st child:
local carNode = xml:first_child()

-- Get the value of the "model" attribute:
print("model = " .. carNode:get_attr_value("model"))

-- Get the value of the "hp" attribute as an integer:
local horsepower = carNode:get_attr_value_int("hp")
print("horsepower = " .. horsepower)

-- Iterate over the attributes and show the name/value of each:
local numAttr = carNode.num_attributes

local i = 0
while i < numAttr do
    print(carNode:get_attribute_name(i) .. ": " .. carNode:get_attribute_value(i))
    i = i + 1
end