Chilkat
XML Parser Quick Start
Click
HERE for a quick overview of the
Chilkat XML object model.
Chilkat
XML provides a simple programming API that with features based
on what developers most commonly need in real-world applications.
For example, XML is often used to save configuration settings.
Here is a code snippet in VB to read a config file, update a value,
and save it.
Sample
XML Config File
|
<?xml
version="1.0" encoding="utf-8" ?>
<config>
<server>mail.chilkatsoft.com</server>
<protocol>POP3</protocol>
<port>110</port>
<login>matt</login>
<password>something</password>
</config>
|
VB Code
to Read XML Config File
' Modify the server in the above XML config file.
Dim xml As New ChilkatXml
xml.LoadXmlFile "config.xml"
if (xml.GetChildContent("server") = "mail.chilkatsoft.com") then
xml.FindChild("server").Content = "pop3.chilkatsoft.com"
end if
xml.SaveXml "config.xml"
|
Simple,
straightforward, and to-the-point. What about doing something
like embedding a GIF image in an XML document?
VB Code
to Embed a GIF Image into an XML Document
Dim gifData As Variant
... load the byte data for the GIF image ...
Dim xml As New ChilkatXml xml.Tag = "my_gif"
zipCompressFlag = 0
encryptFlag = 0
password = ""
xml.SetBinaryContent zipCompressFlag,encryptFlag,password,gifData
xml.SaveXml "myGif.xml"
' This code creates the XML shown below:
|
XML
Output Document with Embedded GIF Image
<?xml version="1.0" encoding="utf-8" ?>
<my_gif>
R0lGODlhZABkAPf/AP////f39+fn7+/v9/f3/+fn99bW597e79bW78bG3s7O57291rW1zsbG
57293rW11q2tzqWlxq2t1qWlzpycxpSUvYyMtYSErZyczpSUxoyMvYSEtXt7rYSEvXt7tXNz
rWtrpWNjnFpalGNjpVJSjFpanEpKhFJSlEJCe0pKjEJChDk5czk5ezExazExcykpYykpayEh
...
</my_gif>
|
How
easy is it to iterate over records? Here is sample XML and C#
code that adds all the company names to a ListBox.
Sample
XML Company Data
<?xml version="1.0" encoding="iso-8859-1" ?>
<companies>
<company>
<name>Chilkat Software, Inc.</name>
<city>Wheaton</city>
<state>IL</state>
<website>http://www.chilkatsoft.com</website>
</company>
<company>
<name>Microsoft Corporation</name>
<city>Redmond</city>
<state>WA</state>
<website>http://www.microsoft.com</website>
</company>
<company>
<name>Intel Corporation</name>
<city>Santa Clara</city>
<state>CA</state>
<website>http://www.intel.com</website>
</company>
<company>
<name>Adobe Systems Incorporated</name>
<city>San Jose</city>
<state>CA</state>
<website>http://www.adobe.com</website>
</company>
<company>
<name>Apple Computer, Inc.</name>
<city>Cupertino</city>
<state>CA</state>
<website>http://www.apple.com</website>
</company>
</companies>
|
C# Code
to Iterate over Company XML Records and Add Names to a ListBox
Chilkat.Xml xml = new Chilkat.Xml();
xml.LoadXmlFile("companies.xml");
Chilkat.Xml company = xml.FirstChild();
while (company != null)
{
listBox1.Items.Add(company.GetChildContent("name"));
company = company.NextSibling();
}
|
Here
this code to sort the company records by name.
C# Code
to Sort Company Records by Company Name
Chilkat.Xml xml = new Chilkat.Xml();
xml.LoadXmlFile("companies.xml");
xml.SortRecordsByContent("name",true);
xml.SaveXml("companies.xml");
|
One
might use the in-memory Zip compression features to compress sub-trees.
The following code iterates over the sample XML company data,
moves each "name" child so that it is an attribute of
the parent, and compresses each company's data.
Visual
Basic code to Zip compress XML sub-trees using the company data
(above)
Dim xml As New ChilkatXml
Dim child As ChilkatXml
xml.LoadXmlFile "company.xml"
' Navigate without creating new objects.
' The "2" methods update the XML object's internal reference
xml.FirstChild2
Do
Set child = xml.FindChild("name")
xml.AddAttribute "name", child.Content
' Remove the "name" node from the XML document.
child.RemoveFromTree
xml.CompressSubtree
Loop Until xml.NextSibling2 = 0
xml.GetParent2
xml.SaveXml "company2.xml"
|
The
code above produces this XML output. Notice that the CDATA is
automatically added. Chilkat XML decides when CDATA is necessary,
and automatically encapsulates your content in CDATA when necessary.
(It also provides a mechanism where you can explicitly control
CDATA.)
Output
XML data with Zip compressed sub-trees.
<?xml version="1.0" encoding="iso-8859-1" ?>
<companies>
<company name="Chilkat Software, Inc.">
<![CDATA[NY2xCsMwDAV3f4XJXKy9KF46BbJ16KwYFZsmdqgFIn8fE5PtHjzuMJRtp3zYTBuPwyum9Udi
3+UrSn9+2CkHN3gMSQ7/iUxSMsK1DFYhYT/NCJ0MKi81NYoi+xNAVV3oytqMrsUQ7o9pnh73
5gQ=
]]>
</company>
<company name="Microsoft Corporation">
<![CDATA[NY2xCoAgFEV3v0L6AN8eLyGaW1qazYwc9Ik+EP8+KdrOhcs5aCkkE5uMJrhpWL3NVOhiuVBO
lA17ioNG67npzZ2B4onwLoGFDTu9zwgfCazuKL7TzZxGgFqrCr9R9RLC/xDd8pW1eAA=
]]>
</company>
<company name="Intel Corporation">
<![CDATA[NY1BCoAgEEX3nkI6QLOPSQhXrTvBJEJCjqID0u2zot3jf3gPXYqZ+NJM0c/DyuJPbVPJqZCE
xINBF+QyG7GQticVQngXhVVIvLELwkcKm99r6HSI5AmgtTaGxzj2CsL/qm74qkbd
]]>
</company>
<company name="Adobe Systems Incorporated">
<![CDATA[NY1BCoQwDEX3PUXxAGYvmYLMSreeINbAuGhTTKD09nZGZvc+D97HKKlQbj5T4tcwH7Kz35oa
J/VLjnIVucj4GALG01rYKPtVlBF+06Fa1+E9IzzksPKuZ6ePWZkAaq0jfbtj/0L4W9cLz3dw
Nw==
]]>
</company>
<company name="Apple Computer, Inc.">
<![CDATA[NY1NCoAgEEb3nkJah7OPSRBXHcNkIKF0qAnp9lnR7vuB9zCWjUO+dA4bjZ1jXkn7tp1Ce6+n
HE1nMSa5rD+Zdkm5ILxd4SFByHqH8CWFleYjtbSI8ABQazXhQZqmQfhf1Qif1qob
]]>
</company>
</companies>
|
Now
we'll show how, using the company data with compressed sub-trees,
to find a particular company, re-inflate the sub-tree dyanmically,
and display the company website address.
Visual
Basic code to decompress a sub-tree on demand.
Dim xml As New ChilkatXml
xml.LoadXmlFile "company2.xml"
' Quickly find the Microsoft company record.
' Find the first node in the document having
' the "company" tag with a "name" attribute
' where the "name" is "Microsoft Corporation"
Dim companyRoot As ChilkatXml
Set companyRoot = xml.SearchForAttribute _
(Nothing, "company", "name", "Microsoft Corporation")
' Decompress the sub-tree.
companyRoot.DecompressSubtree
' Now get the website address.
Text1.Text = companyRoot.GetChildContent("website")
|