Tutorial of LBXML Operator, a C# API-Based Tool for XML Insertion, Modification, Searching, and Remo

1. Configurations for LBXML Operator

LBXML Operator runs over the .NET platform. 2.0, the latest version of LBXML Operator, has been tested successfully over the .NET 2003 platform.

It is easy to install LBXML Operator. Two DLLs, as follow, are what you need to care about. To install it, what you need to do is to make just two references in your C# applications for the two DLLs.

com.lblabs.tools.csharp.dll
com.lblabs.xmltool.dll

To program with LBXML Operator, declare the following statements at the beginning of your C# code:

using com.lblabs.xmltool.csharp;
using com.lblabs.xmltool;

It is noted that LBXML Operator is just developed in an initial phase. In the further versions, more types of operations will be designed to support more powerful functionalities.

Another issue is that LBXML Operator is not responsible for validating the XML file. So, when using any APIs in LBXML Operator, users need to make sure that the XML file must be validated after the particular operation, such as modification, insertion, and removal. Otherwise, the XML parser may interrupt the system execution.

2. Examples of Using LBXML Operator

The operations on XML are categorized into four types: insertion, modification, searching, and removal. This section discusses the four types of operation one by one. All the operations are included in the CSharpLBXMLOperator class.

To make a clear explanation, a typical XML file is selected and example of operations are shown on it. The XML and its DTD are listed as follows.

<?xml version="1.0"?>
<!DOCTYPE RequirementInSAT SYSTEM "requirement_in_sat.dtd" >
<RequirementInSAT>
   <Version>2.10</Version>
   <NewSAT>
      <SAT>
         <Organization>BigBug.com</Organization>
         <User>customer</User>
         <Workflow>Customer-Retailer</Workflow>
         <Form>
            <Request>OrderForm</Request>
            <Response>ReceiptForm</Response>
         </Form>
        <Vocabulary>
           <VocabularyName>OrderedNumber</VocabularyName>
           <VocabularyName>InputCreditCardNumber</VocabularyName>
         </Vocabulary>
         <Contract>
            <ContractName>WholesaleContract</ContractName>
            <ContractName>CreditCheckingContract</ContractName>
         </Contract>
      </SAT>
   </NewSAT>
   <ExistingSAT>
      <SAT>
         <Organization>RequiredCreditChecking.com</Organization>
         <User>guest</User>
         <Workflow>Customer-Creditor</Workflow>
         <Form>
            <Request>CreditRequestForm</Request>
            <Response>CreditResponseForm</Response>
         </Form>
         <Vocabulary>
            <VocabularyName>OrderedNumber</VocabularyName>
            <VocabularyName>CreditRequestNo</VocabularyName>
         </Vocabulary>
         <Contract>
            <ContractName>CreditCheckingContract</ContractName>
         </Contract>
      </SAT>
   </ExistingSAT>
</RequirementInSAT>

List 1. The XML file used as an example in the article

<!ELEMENT RequirementInSAT (Version, NewSAT*, ExistingSAT*)>
<!ELEMENT Version (#PCDATA)>
<!ELEMENT NewSAT (SAT*)>
<!ELEMENT ExistingSAT (SAT*)>
<!ELEMENT SAT (Organization*, User*, Workflow*, Form*, Vocabulary*,
               Contract*)>
<!ELEMENT Organization (#PCDATA)>
<!ELEMENT User (#PCDATA)>
<!ELEMENT Workflow (#PCDATA)>
<!ELEMENT Form (Request, Response)>
<!ELEMENT Request (#PCDATA)>
<!ELEMENT Response (#PCDATA)>
<!ELEMENT Vocabulary (VocabularyName*)>
<!ELEMENT VocabularyName (#PCDATA)>
<!ELEMENT Contract (ContractName*)>
<!ELEMENT ContractName (#PCDATA)>

List 2. DTD for the XML file to be used as an example in the article

2.1 Insertion

The latest version of LBXML Operator provides two types of XML insertions. They are insertNodeByParentTag() and insertNodeByParentTagAndParentSibling().

2.1.1 insertNodeByParentTag()

When using this method to insert a tag and its value into an XML, you mudy specify the parent tag, the tag to be inserted and the value of the tag. The format of the method is defined as follows.

bool insertNodeByParentTag(String xmlFile, String parentTag,
                           String updateTag, String updateValue)

For example, if a tag, VocabularyName, and its value, CurrentCash, need to be inserted into the XML file of List 1, the code is listed as follows.

CSharpLBXMLOperator lbXMLOperator = new CSharpLBXMLOperator();
lbXMLOperator.insertNodeByParentTag("./xmlfile.xml", "Vocabulary",
                                    "VocabularyName",
                                    "CurrentCash");

After the operation, the XML file of List 1 is changed. The new tag, VocabularyName, and its value, CurrentCash, are inserted below the Vocabulary tag. Because the Vocabulary tag appears two times in the XML file, the inserted tag, VocabularyName and its value, CurrentCash, also are inserted two times. The XML file after the operation is shown as follows.

<?xml version="1.0"?>
<!DOCTYPE RequirementInSAT SYSTEM "requirement_in_sat.dtd" >
<RequirementInSAT>
   <Version>2.10</Version>
   <NewSAT>
      <SAT>
         <Organization>BigBug.com</Organization>
         <User>customer</User>
         <Workflow>Customer-Retailer</Workflow>
         <Form>
            <Request>OrderForm</Request>
            <Response>ReceiptForm</Response>
         </Form>
         <Vocabulary>
            <VocabularyName>OrderedNumber</VocabularyName>
            <VocabularyName>InputCreditCardNumber</VocabularyName>
            <VocabularyName>CurrentCash</VocabularyName>
         </Vocabulary>
         <Contract>
            <ContractName>WholesaleContract</ContractName>
            <ContractName>CreditCheckingContract</ContractName>
         </Contract>
      </SAT>
   </NewSAT>
   <ExistingSAT>
      <SAT>
         <Organization>RequiredCreditChecking.com</Organization>
         <User>guest</User>
         <Workflow>Customer-Creditor</Workflow>
         <Form>
            <Request>CreditRequestForm</Request>
            <Response>CreditResponseForm</Response>
         </Form>
         <Vocabulary>
            <VocabularyName>OrderedNumber</VocabularyName>
            <VocabularyName>CreditRequestNo</VocabularyName>
            <VocabularyName>CurrentCash</VocabularyName>
         </Vocabulary>
         <Contract>
            <ContractName>CreditCheckingContract</ContractName>
         </Contract>
      </SAT>
   </ExistingSAT>
</RequirementInSAT>

List 3. The XML file after the operation, insertNodeByParentTag()

2.1.2 insertNodeByParentTagAndParentSibling()

It is possible that the tag, Vocabulary, and its value, CurrentCash, should be inserted only below the first tag, Vocabulary. Under this situation, the method is insertNodeByParentTagAndParentSibling(). The format is shown as follows.

bool insertNodeByParentTagAndParentSibling(String xmlFile,
     String parentTag, String parentSiblingTag,
     String parentSiblingValue, String updateTag,
     String updateValue)

The code to use the above method is shown as follows.

CSharpLBXMLOperator lbXMLOperator = new CSharpLBXMLOperator();
lbXMLOperator.insertNodeByParentTagAndParentSibling("./xmlfile.xml",
              "Vocabulary", "Organization", "BigBug.com",
              "VocabularyName", "CurrentCash");

The difference between insertNodeByParentTagAndParentSibling() and insertNodeByParentTag() is that new parameters, parentSiblingTag and parentSiblingValue, are specified. Through the two parameters, the LBXML Operator is able to figure out the position to insert the new tag and its value. The following list shows in the new XML after the operation.

<?xml version="1.0"?>
<!DOCTYPE RequirementInSAT SYSTEM "requirement_in_sat.dtd" >
<RequirementInSAT>
   <Version>2.10</Version>
   <NewSAT>
      <SAT>
         <Organization>BigBug.com</Organization>
         <User>customer</User>
         <Workflow>Customer-Retailer</Workflow>
         <Form>
            <Request>OrderForm</Request>
            <Response>ReceiptForm</Response>
         </Form>
         <Vocabulary>
            <VocabularyName>OrderedNumber</VocabularyName>
            <VocabularyName>InputCreditCardNumber</VocabularyName>
            <VocabularyName>CurrentCash</VocabularyName>
         </Vocabulary>
         <Contract>
            <ContractName>WholesaleContract</ContractName>
            <ContractName>CreditCheckingContract</ContractName>
         </Contract>
      </SAT>
   </NewSAT>
   <ExistingSAT>
      <SAT>
         <Organization>RequiredCreditChecking.com</Organization>
         <User>guest</User>
         <Workflow>Customer-Creditor</Workflow>
         <Form>
            <Request>CreditRequestForm</Request>
            <Response>CreditResponseForm</Response>
         </Form>
         <Vocabulary>
            <VocabularyName>OrderedNumber</VocabularyName>
            <VocabularyName>CreditRequestNo</VocabularyName>
         </Vocabulary>
         <Contract>
            <ContractName>CreditCheckingContract</ContractName>
         </Contract>
      </SAT>
   </ExistingSAT>
</RequirementInSAT>

List 4. The XML file after the operation, insertNodeByParentTagAndParentSibling()

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read