Mercurial > minori
comparison dep/pugixml/docs/samples/modify_base.cpp @ 55:d10b6c6b432e
add xml lib, we will need to use it eventually
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Tue, 26 Sep 2023 12:37:08 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 54:466ac9870df9 | 55:d10b6c6b432e |
|---|---|
| 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 |
