uses
OmniXML,
OmniXMLUtils;
procedure ProcessNode(const node: IXMLNode; nodeText: TStrings);
procedure CollectNodes(const node: IXMLNode; const nodeList: IXMLNodeList);
var
childNode: IXMLNode;
iChild : integer;
begin
for iChild := 0 to node.ChildNodes.Length-1 do begin
childNode := node.ChildNodes.Item[iChild];
if childNode.NodeType = TEXT_NODE then
nodeText.Add(childNode.NodeValue)
else if childNode.NodeType = ELEMENT_NODE then begin
nodeText.Add(childNode.NodeName);
CollectNodes(childNode, nodeList);
nodeText.Add('/' + childNode.NodeName);
end;
end;
end; { CollectNodes }
var
childNode: IXMLNode;
nodeList : IXMLNodeList;
begin
nodeList := TXMLNodeList.Create;
CollectNodes(node, nodeList);
end; { ProcessNode }
procedure TForm39.FormCreate(Sender: TObject);
var
xml: IXMLDocument;
nodeText: TStringList;
begin
xml := CreateXMLDoc;
if XMLLoadFromString(xml, '<test><p> Normal text <b> bolded </b> finish normal text </p></test>') then begin
nodeText := TStringList.Create;
try
ProcessNode(xml.SelectSingleNode('test'), nodeText);
finally FreeAndNil(nodeText); end;
end;
end;
会给你:
p
Normal text
b
bolded
/b
finish normal text
/p