aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/xml/document/README
blob: 69f83c55e600ff50235aff6c85e73ddee8599d91 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
A wrapper around the DOM interface of libxml2.

The standard way to use it is as follows:

    #include <library/cpp/xml/document/xml-document.h>
    ...

    // open a document
    NXml::TDocument xml("filename.xml");

    // get a nodeset from an XPath query
    NXml::TConstNodes nodes = xml.Root().Nodes("xpath/expression/here");

    // iterate over the nodeset
    for (size_t i = 0; i < nodes.size(); ++i) {
        using namespace NXml; 
        TConstNode& node = nodes[i];
        // query node 
        TString name = node.Name();
        TString lang = node.Attr<TString>("lang");
        TString text = node.Value<TString>();
        TConstNode child = node.GetFirstChild(""); 
        // edit node 
        TNode node = child.ConstCast(); 
        node.DelAttr("id"); 
        node.SetAttr("x", 2); 
        node.SetValue(5); 
        node.AddText(" apples"); 
    }

    // edit documents with copy-paste 
    NXml::TDocument xml2("<xpath><node/></xpath>", NXml::TDocument::String); 
    NXml::TNode place = xml2.Root().Node("xpath/node"); 
    // copy node's subtree from one document to another 
    place.AddChild(xml.Root()); 
    // save (render) single element 
    TString modifiedNode = place.ToString();
    // save whole document with optional encoding 
    TString modifiedDoc  = xml2.ToString("ISO-8559-1");
 
 
See xml-document_ut.cpp for more examples.