[459] | 1 | unit XMLUtils;
|
---|
| 2 |
|
---|
| 3 | interface
|
---|
| 4 | uses
|
---|
| 5 | SysUtils, MSXML_TLB, ORFn;
|
---|
| 6 |
|
---|
| 7 | function Text2XML(const AText: string): string;
|
---|
| 8 | function FindXMLElement(Element: IXMLDOMNode; ElementTag: string): IXMLDOMNode;
|
---|
| 9 | function FindXMLAttributeValue(Element: IXMLDOMNode; AttributeTag: string): string;
|
---|
| 10 | function GetXMLWPText(Element: IXMLDOMNode): string;
|
---|
| 11 |
|
---|
| 12 | implementation
|
---|
| 13 |
|
---|
| 14 | type
|
---|
| 15 | TXMLChars = (xcAmp, xcGT, xcLT, xcApos, xcQuot); // Keep & first in list!
|
---|
| 16 |
|
---|
| 17 | const
|
---|
| 18 | XMLCnvChr: array[TXMLChars] of string = ('&', '>', '<', '''', '"');
|
---|
| 19 | XMLCnvStr: array[TXMLChars] of string = ('&','>','<',''','"');
|
---|
| 20 |
|
---|
| 21 | function Text2XML(const AText: string): string;
|
---|
| 22 | var
|
---|
| 23 | i: TXMLChars;
|
---|
| 24 | p: integer;
|
---|
| 25 | tmp: string;
|
---|
| 26 |
|
---|
| 27 | begin
|
---|
| 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;
|
---|
| 43 | end;
|
---|
| 44 |
|
---|
| 45 | function FindXMLElement(Element: IXMLDOMNode; ElementTag: string): IXMLDOMNode;
|
---|
| 46 | var
|
---|
| 47 | Children: IXMLDOMNodeList;
|
---|
| 48 | Child: IXMLDOMNode;
|
---|
| 49 | i, count: integer;
|
---|
| 50 |
|
---|
| 51 | begin
|
---|
| 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;
|
---|
| 67 | end;
|
---|
| 68 |
|
---|
| 69 | function FindXMLAttributeValue(Element: IXMLDOMNode; AttributeTag: string): string;
|
---|
| 70 | var
|
---|
| 71 | Attributes: IXMLDOMNamedNodeMap;
|
---|
| 72 | Attribute: IXMLDOMNode;
|
---|
| 73 | i, count: integer;
|
---|
| 74 |
|
---|
| 75 | begin
|
---|
| 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;
|
---|
| 96 | end;
|
---|
| 97 |
|
---|
| 98 | function GetXMLWPText(Element: IXMLDOMNode): string;
|
---|
| 99 | var
|
---|
| 100 | Children: IXMLDOMNodeList;
|
---|
| 101 | Child: IXMLDOMNode;
|
---|
| 102 | i, count: integer;
|
---|
| 103 |
|
---|
| 104 | begin
|
---|
| 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;
|
---|
| 126 | end;
|
---|
| 127 |
|
---|
| 128 | end.
|
---|