Chilkat HOME ASP Visual Basic VB.NET C# Visual C++ C MFC Delphi FoxPro Java Perl PHP Python Ruby SQL Server VBScript
|
Remove Nodes from an XML Document Source Code Listing // Delete Keyword nodes from the XML Document, and
// add them to a new document without duplicates.
procedure TForm1.Example4Click(Sender: TObject);
var
xml: IChilkatXml;
outXml: IChilkatXml;
node: IChilkatXml;
kNode: IChilkatXml;
tempNode: IChilkatXml;
begin
xml := CoChilkatXml.Create();
outXml := CoChilkatXml.Create();
// Load the input document.
xml.LoadXmlFile('crisp.xml');
// Create the output XML document in-memory
outXml.Tag := 'unique_keywords';
// Iterate over the "DOC" nodes by calling FirstChild
// followed by NextSibling2 repeatedly until it returns false.
node := xml.FirstChild();
while (node <> nil) do
begin
// Iterate over the Keyword nodes, remove each, and add it to
// our new XML document. (Note that the search happens at the
// subtree rooted at our current node, and not the entire XML document.)
kNode := node.SearchForTag(nil,'Keyword');
while (kNode <> nil) do
begin
// Does the keyword already exist in the output document?
if (outXml.SearchForContent(nil,'keyword',kNode.Content) = nil) then
begin
outXml.NewChild2('keyword',kNode.Content);
end;
tempNode := kNode;
kNode := node.SearchForTag(kNode,'Keyword');
// Remove the keyword that was just processed from the XML document.
tempNode.RemoveFromTree();
end;
// Move to the next sibling. The internal reference within node is updated
// to the node's next sibling. If no siblings remain, it returns 0.
if (node.NextSibling2() = 0) then
node := nil;
end;
// Sort the output by content.
outXml.SortByContent(1);
// Save the unique keywords.
outXml.SaveXml('UniqueKeywords.xml');
// Save the original document with all the keywords removed.
xml.SaveXml('KeywordsRemoved.xml');
end;
|
Need a specific example? Send a request to support@chilkatsoft.com
© 2000-2008 Chilkat Software, Inc. All Rights Reserved.