Zig
Zig
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 Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
const xml = try chilkat.Xml.init();
defer xml.deinit();
var i: i32 = 0;
// The sample input XML is available at http://www.chilkatsoft.com/data/car.xml
xml.loadXmlFile("car.xml") catch {
std.debug.print("{s}\n", .{try xml.getLastErrorText(alloc)});
return;
};
// Navigate to the "car" node, which is the 1st child:
const car_node = try xml.firstChild();
defer car_node.deinit();
// Get the value of the "model" attribute:
std.debug.print("model = {s}\n", .{try car_node.getAttrValue(alloc, "model")});
// Get the value of the "hp" attribute as an integer:
const horsepower = car_node.getAttrValueInt("hp");
std.debug.print("horsepower = {d}\n", .{horsepower});
// Iterate over the attributes and show the name/value of each:
const num_attr = car_node.getNumAttributes();
i = 0;
while (i < num_attr) {
std.debug.print("{s}: {s}\n", .{ try car_node.getAttributeName(alloc, i), try car_node.getAttributeValue(alloc, i) });
i = i + 1;
}
}