comparison dep/pugixml/docs/samples/modify_base.cpp @ 367:8d45d892be88 default tip

*: instead of pugixml, use Qt XML features this means we have one extra Qt dependency though...
author Paper <paper@tflc.us>
date Sun, 17 Nov 2024 22:55:47 -0500
parents 886f66775f31
children
comparison
equal deleted inserted replaced
366:886f66775f31 367:8d45d892be88
1 #include "pugixml.hpp"
2
3 #include <string.h>
4 #include <iostream>
5
6 int main()
7 {
8 pugi::xml_document doc;
9 if (!doc.load_string("<node id='123'>text</node><!-- comment -->", pugi::parse_default | pugi::parse_comments)) return -1;
10
11 // tag::node[]
12 pugi::xml_node node = doc.child("node");
13
14 // change node name
15 std::cout << node.set_name("notnode");
16 std::cout << ", new node name: " << node.name() << std::endl;
17
18 // change comment text
19 std::cout << doc.last_child().set_value("useless comment");
20 std::cout << ", new comment text: " << doc.last_child().value() << std::endl;
21
22 // we can't change value of the element or name of the comment
23 std::cout << node.set_value("1") << ", " << doc.last_child().set_name("2") << std::endl;
24 // end::node[]
25
26 // tag::attr[]
27 pugi::xml_attribute attr = node.attribute("id");
28
29 // change attribute name/value
30 std::cout << attr.set_name("key") << ", " << attr.set_value("345");
31 std::cout << ", new attribute: " << attr.name() << "=" << attr.value() << std::endl;
32
33 // we can use numbers or booleans
34 attr.set_value(1.234);
35 std::cout << "new attribute value: " << attr.value() << std::endl;
36
37 // we can also use assignment operators for more concise code
38 attr = true;
39 std::cout << "final attribute value: " << attr.value() << std::endl;
40 // end::attr[]
41 }
42
43 // vim:et