File: examplea.c

package info (click to toggle)
gdome2 0.8.1%2Bdebian-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,492 kB
  • ctags: 1,947
  • sloc: ansic: 23,268; sh: 6,710; xml: 476; makefile: 474
file content (112 lines) | stat: -rw-r--r-- 3,700 bytes parent folder | download | duplicates (9)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
/* examplea.c
 *
 * Copyright (C) 2001 Paolo Casarini <paolo@casarini.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdio.h>
#include <gdome.h>

int main (int argc, char **argv) {
	GdomeDOMImplementation *domimpl;
	GdomeDocument *doc;
	GdomeElement *root, *el;
	GdomeText *txtnode;
	GdomeException exc;
	GdomeNode *result;
	GdomeDOMString *name, *value;

	/* First I get a DOMImplementation reference */
  domimpl = gdome_di_mkref ();

	/* I create a new document with TEST as root element */
	name = gdome_str_mkref ("TEST");
	doc = gdome_di_createDocument(domimpl, NULL, name, NULL, &exc);
	if (doc == NULL) {
		fprintf (stderr, "DOMImplementation.createDocument: failed\n\tException #%d\n", exc);
		return 1;
	}
	gdome_str_unref (name);

	/* I get reference to the root element of the document */
	root = gdome_doc_documentElement (doc, &exc);
	if (root == NULL) {
		fprintf (stderr, "Document.documentElement: NULL\n\tException #%d\n", exc);
		return 1;
	}

	/* I set an attribute to the root element */
	name = gdome_str_mkref ("PACKAGE");
	value = gdome_str_mkref ("gdome2");
	gdome_el_setAttribute (root, name, value, &exc);
	if (exc) {
		fprintf (stderr, "Element.setAttribute: failed\n\tException #%d\n", exc);
		return 1;
	}
	gdome_str_unref (name);
	gdome_str_unref (value);

	/* I create a new element called RELEASE */
	name = gdome_str_mkref ("RELEASE");
	el = gdome_doc_createElement (doc, name, &exc);
	if (el == NULL) {
		fprintf (stderr, "Document.createElement: NULL\n\tException #%d\n", exc);
		return 1;
	}
	gdome_str_unref (name);

	/* I create a Text Node*/
	value = gdome_str_mkref ("0.6.x");
	txtnode = gdome_doc_createTextNode (doc, value, &exc);
	if (txtnode == NULL) {
		fprintf (stderr, "Document.createTextNode: NULL\n\tException #%d\n", exc);
		return 1;
	}
	gdome_str_unref (value);

	/* I append the Text Node created to the childs list of the RELEASE element */
	result = gdome_el_appendChild (el, (GdomeNode *)txtnode, &exc);
	if (result != (GdomeNode *)txtnode) {
		fprintf (stderr, "Element.appendChild: failed\n\tException #%d\n", exc);
		return 1;
	}
	gdome_t_unref(txtnode, &exc);
	gdome_n_unref(result, &exc);

	/* I append the RELEASE element to the childs list of the root element */
	result = gdome_el_appendChild (root, (GdomeNode *)el, &exc);
	if (result != (GdomeNode *)el) {
		fprintf (stderr, "Element.appendChild: failed\n\tException #%d\n", exc);
		return 1;
	}
	gdome_el_unref(el, &exc);
	gdome_el_unref(root, &exc);
	gdome_n_unref(result, &exc);

	/* I save the  created document to a file named "examplea.xml */
	if (!gdome_di_saveDocToFile (domimpl, doc, LOCALDIR"/examplea.xml", GDOME_SAVE_STANDARD, &exc)) {
		fprintf (stderr, "DOMImplementation.saveDocToFile: failed\n\tException #%d\n", exc);
		return 1;
	}

	/* I free the document structure and the DOMImplementation */
	gdome_doc_unref (doc, &exc);
	gdome_di_unref (domimpl, &exc);

	return 0;
}