2
0

tree2.pas 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. (*
  2. * section: Tree
  3. * synopsis: Creates a tree
  4. * purpose: Shows how to create document, nodes and dump it to stdout or file.
  5. * usage: tree2 <filename> -Default output: stdout
  6. * test: tree2 > tree2.tmp ; diff tree2.tmp tree2.res ; rm tree2.tmp
  7. * author: Lucas Brasilino <[email protected]>
  8. * copy: see Copyright for the status of this software
  9. *)
  10. program tree2;
  11. {$mode objfpc}
  12. uses
  13. ctypes,
  14. xml2,
  15. exutils,
  16. SysUtils;
  17. var
  18. doc: xmlDocPtr;
  19. root_node, node, node1: xmlNodePtr;
  20. dtd: xmlDtdPtr;
  21. buff: array[0..255] of char;
  22. i, j: cint;
  23. begin
  24. LIBXML_TEST_VERSION;
  25. (*
  26. * Creates a new document, a node and set it as a root node
  27. *)
  28. doc := xmlNewDoc('1.0');
  29. root_node := xmlNewNode(nil, 'root');
  30. xmlDocSetRootElement(doc, root_node);
  31. (*
  32. * Creates a DTD declaration. Isn't mandatory.
  33. *)
  34. dtd := xmlCreateIntSubset(doc, 'root', nil, 'tree2.dtd');
  35. (*
  36. * xmlNewChild() creates a new node, which is "attached" as child node
  37. * of root_node node.
  38. *)
  39. xmlNewChild(root_node, nil, 'node1', 'content of node 1');
  40. (*
  41. * The same as above, but the new child node doesn't have a content
  42. *)
  43. xmlNewChild(root_node, nil, 'node2', nil);
  44. (*
  45. * xmlNewProp() creates attributes, which is "attached" to an node.
  46. * It returns xmlAttrPtr, which isn't used here.
  47. *)
  48. node := xmlNewChild(root_node, nil, 'node3', 'this node has attributes');
  49. xmlNewProp(node, 'attribute', 'yes');
  50. xmlNewProp(node, 'foo', 'bar');
  51. (*
  52. * Here goes another way to create nodes. xmlNewNode() and xmlNewText
  53. * creates a node and a text node separately. They are "attached"
  54. * by xmlAddChild()
  55. *)
  56. node := xmlNewNode(nil, 'node4');
  57. node1 := xmlNewText('other way to create content (which is also a node)');
  58. xmlAddChild(node, node1);
  59. xmlAddChild(root_node, node);
  60. (*
  61. * A simple loop that "automates" nodes creation
  62. *)
  63. for i := 5 to 6 do
  64. begin
  65. buff := 'node'+inttostr(i);
  66. node := xmlNewChild(root_node, nil, buff, nil);
  67. for j := 1 to 3 do
  68. begin
  69. buff := 'node'+inttostr(i)+inttostr(j);
  70. node1 := xmlNewChild(node, nil, buff, nil);
  71. if j mod 2 = 0 then
  72. xmlNewProp(node1, 'odd', 'no')
  73. else
  74. xmlNewProp(node1, 'odd', 'yes');
  75. end;
  76. end;
  77. (*
  78. * Dumping document to stdio or file
  79. *)
  80. docdump(doc);
  81. //xmlSaveFormatFileEnc(argc > 1 ? argv[1] : "-", doc, "UTF-8", 1);
  82. (*free the document *)
  83. xmlFreeDoc(doc);
  84. (*
  85. * Free the global variables that may
  86. * have been allocated by the parser.
  87. *)
  88. xmlCleanupParser();
  89. (*
  90. * this is to debug memory for regression tests
  91. *)
  92. xmlMemoryDump();
  93. end.