Zig
Zig
Demonstrate the XML FindNextRecord Method
See more XML Examples
Imagine we have an XML file as follows:
<CompleteMultipartUpload>
<Part>
<PartNumber>1</PartNumber>
<ETag>abc</ETag>
</Part>
<Part>
<PartNumber>2</PartNumber>
<ETag>def</ETag>
</Part>
<Part>
<PartNumber>3</PartNumber>
<ETag>ghi</ETag>
</Part>
</CompleteMultipartUpload>
We want to find the record where PartNumber = 2. To do so, we simply position ourselves at the first "Part" tag, and then call FindNextRecord. (See below..)
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();
xml.loadXmlFile("qa_data/xml/multipartUpload.xml") catch {};
// Position ourselves at the 1st record.
xml.getChild2(0) catch {};
var found_rec: chilkat.Xml = try xml.findNextRecord("PartNumber", "2");
defer found_rec.deinit();
if (xml.getLastMethodSuccess()) {
std.debug.print("Found the record where PartNumber = 2.\n", .{});
std.debug.print("{s}\n", .{try found_rec.getXml(alloc)});
// Let's examine the ETag for this record...
std.debug.print("ETag for PartNumber 2 = {s}\n", .{try found_rec.getChildContent(alloc, "ETag")});
} else {
std.debug.print("No record exists where PartNumber = 2.\n", .{});
}
// What if we want to find the record where ETag = "abc"?
{
const next = try xml.findNextRecord("ETag", "abc");
found_rec.deinit();
found_rec = next;
}
if (xml.getLastMethodSuccess()) {
std.debug.print("Found the record where ETag = abc\n", .{});
std.debug.print("{s}\n", .{try found_rec.getXml(alloc)});
// Let's examine the PartNumber for this record...
std.debug.print("Part number for Etag(abc) = {s}\n", .{try found_rec.getChildContent(alloc, "PartNumber")});
} else {
std.debug.print("No record exists where ETag = abc.\n", .{});
}
// Go back to the XML root..
xml.getRoot2();
}