source: cprs/branches/tmg-cprs/CPRS-Lib/XMLUtils.pas@ 820

Last change on this file since 820 was 453, checked in by Kevin Toppenberg, 16 years ago

Initial upload of TMG-CPRS 1.0.26.69

File size: 2.9 KB
RevLine 
[453]1unit XMLUtils;
2
3interface
4uses
5 SysUtils, MSXML_TLB, ORFn;
6
7function Text2XML(const AText: string): string;
8function FindXMLElement(Element: IXMLDOMNode; ElementTag: string): IXMLDOMNode;
9function FindXMLAttributeValue(Element: IXMLDOMNode; AttributeTag: string): string;
10function GetXMLWPText(Element: IXMLDOMNode): string;
11
12implementation
13
14type
15 TXMLChars = (xcAmp, xcGT, xcLT, xcApos, xcQuot); // Keep & first in list!
16
17const
18 XMLCnvChr: array[TXMLChars] of string = ('&', '>', '<', '''', '"');
19 XMLCnvStr: array[TXMLChars] of string = ('&amp;','&gt;','&lt;','&apos;','&quot;');
20
21function Text2XML(const AText: string): string;
22var
23 i: TXMLChars;
24 p: integer;
25 tmp: string;
26
27begin
28 Result := AText;
29 for i := low(TXMLChars) to high(TXMLChars) do
30 begin
31 tmp := Result;
32 Result := '';
33 repeat
34 p := pos(XMLCnvChr[i],tmp);
35 if(p > 0) then
36 begin
37 Result := Result + copy(tmp,1,p-1) + XMLCnvStr[i];
38 delete(tmp,1,p);
39 end;
40 until(p=0);
41 Result := Result + tmp;
42 end;
43end;
44
45function FindXMLElement(Element: IXMLDOMNode; ElementTag: string): IXMLDOMNode;
46var
47 Children: IXMLDOMNodeList;
48 Child: IXMLDOMNode;
49 i, count: integer;
50
51begin
52 Result := nil;
53 Children := Element.Get_childNodes;
54 if assigned(Children) then
55 begin
56 count := Children.Get_length;
57 for i := 0 to count-1 do
58 begin
59 Child := Children.Get_item(i);
60 if assigned(Child) and (CompareText(Child.Get_nodeName, ElementTag) = 0) then
61 begin
62 Result := Child;
63 break;
64 end;
65 end;
66 end;
67end;
68
69function FindXMLAttributeValue(Element: IXMLDOMNode; AttributeTag: string): string;
70var
71 Attributes: IXMLDOMNamedNodeMap;
72 Attribute: IXMLDOMNode;
73 i, count: integer;
74
75begin
76 Result := '';
77 Attributes := Element.Get_attributes;
78 try
79 if assigned(Attributes) then
80 begin
81 count := Attributes.Get_Length;
82 for i := 0 to Count - 1 do
83 begin
84 Attribute := Attributes.Item[i];
85 if CompareText(Attribute.Get_NodeName, AttributeTag) = 0 then
86 begin
87 Result := Attribute.Get_Text;
88 break;
89 end;
90 end;
91 end;
92 finally
93 Attribute := nil;
94 Attributes := nil;
95 end;
96end;
97
98function GetXMLWPText(Element: IXMLDOMNode): string;
99var
100 Children: IXMLDOMNodeList;
101 Child: IXMLDOMNode;
102 i, count: integer;
103
104begin
105 Result := '';
106 Children := Element.Get_childNodes;
107 try
108 if assigned(Children) then
109 begin
110 count := Children.Length;
111 for i := 0 to Count - 1 do
112 begin
113 Child := Children.Item[i];
114 if CompareText(Child.NodeName, 'P') = 0 then
115 begin
116 if(Result <> '') then
117 Result := Result + CRLF;
118 Result := Result + Child.Get_Text;
119 end;
120 end;
121 end;
122 finally
123 Child := nil;
124 Children := nil;
125 end;
126end;
127
128end.
Note: See TracBrowser for help on using the repository browser.