Menu

[r11]: / branches / dasho / DOM / sample / samp.C  Maximize  Restore  History

Download this file

96 lines (74 with data), 2.2 kB

 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
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <DOM.h>
void printTree(Node* node, int depth);
void printTextNode(Text* txt, int depth);
void main(int argn, char* argv[])
{
if (argn != 3)
{
printf("usage: %s <xmlFile> <dtdFile> \n", argv[0]);
exit(1);
}
char* xmlFile = argv[1];
char* dtdFile = argv[2];
Document* xmlDoc = new Document(xmlFile);
xmlDoc->getImplementation()->docParse(xmlFile, dtdFile);
// print what has been constructed
cout << "\n\nThe XML structure in memory contains: \n\n";
Element* xmlRoot = xmlDoc->getDocumentElement();
if (xmlRoot != NULL) printTree(xmlRoot, 0);
cout << "\n\nThe DTD structure in memory contains: \n\n";
xmlDoc->getDoctype()->print(cout);
delete xmlDoc;
}
void printTree(Node* node, int depth)
{
String name = node->getNodeName();
short type = node->getNodeType();
if (type == Node::TEXT_NODE) printTextNode( (Text*)node, depth);
if (type != Node::ELEMENT_NODE) return;
for (int i=0; i < depth; i++) cout << " ";
cout << "<" << name;
// print attributes
NamedNodeMap* attrList = node->getAttributes();
if (attrList != NULL)
for (int i=0; i < attrList->getLength(); i++)
{
Attr* attr = (Attr*) attrList->item(i);
if (attr != NULL)
cout << " " << attr->getName() << "=" << attr->getValue();
}
cout << ">" << endl;
// print children
Node* child = node->getFirstChild();
while (child != NULL)
{
printTree(child, depth+1);
child = child->getNextSibling();
}
for (int i=0; i < depth; i++) cout << " ";
cout << "</" << name << ">" << endl;
}
void printTextNode(Text* txt, int depth)
{
char* cData = txt->getData();
char cData1[strlen(cData) + 1];
char* cp = cData;
char* cp1 = cData1;
int space = 0;
while (*cp)
{
if ( isspace(*cp) ) space++;
else space = 0;
if (space == 0) *cp1++ = *cp++;
else if (space == 1) { *cp1++ = ' '; cp++; }
else cp++;
}
*cp1 = 0;
if ( space == strlen(cData) ) return;
for (int i=0; i < depth; i++) cout << " ";
cout << cData1 << endl;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.