2.4 Searching
LBXML Operator provides ten methods to search XML files. Similar to other methods in LBXML Operator, searching conditions are specified through method interfaces (APIs). Another feature is that searching results are returned into C# powerful data structures, such as String and Hashtable, which are convenient for further processing.
2.4.1 selectByKeyTag()
selectorByKeyTag() is utilized to search the value of the tag that is unique in an XML file. The format of the method is shown as follows.
String selectByKeyTag(String xmlFile, String keyTag)
For example, to search the value of the Version tag, the code is written as follows.
CSharpLBXMLOperator lbXMLOperator = new CSharpLBXMLOperator();
String version = lbXMLOperator.selectByKeyTag("./xmlfile.xml",
"Version");
Console.WriteLine("version = " + version);
After running the above code, the result is shown as follows.
version = 2.10
2.4.2 selectByTagAndWhere()
selectorByKeyTag() is not suitable to search the value of the tag that is not unique in an XML file. For example, to search the value of the User tag, it is a good idea to use selectByTagAndWhere() because User is not the unique tag in the XML file. The format of the method is shown as follows.
String selectByTagAndWhere(String xmlFile, String keyTag,
String searchTag, String keyValue)
To search the value of the User tag, the code is written as follows.
CSharpLBXMLOperator lbXMLOperator = new CSharpLBXMLOperator();
String user = lbXMLOperator.selectByTagAndWhere("./xmlfile.xml",
"Organization", "User", "BigBug.com");
Console.WriteLine("user = " + user);
After the above is executed, the following result is displayed. The User's value is customer.
user = customer
2.4.3 selectByBelowTagAndWhere()
In the parameters of the selectByTagAndWhere() method, the key tag is beyond the tag to be searched. Sometimes it is probable that the key tag is below the tag to be searched. The selectByBelowTagAndWhere() method is used to handle this problem. The format of the method is shown as follows.
String selectByBelowTagAndWhere(String xmlFile, String belowKeyTag,
String searchTag,
String belowKeyValue)
For example, if users need to search the value of the tag, Organization, the User tag can be used as a key tag. The code is written as follows.
CSharpLBXMLOperator lbXMLOperator = new CSharpLBXMLOperator();
String organization = lbXMLOperator.selectByBelowTagAndWhere(
"./xmlfile.xml", "User", "Organization", "customer");
Console.WriteLine("organization = " + organization);
The following result is displayed after the above code is executed.
organization = BigBug.com
2.4.4 selectHash()
It is always possible that a tag has multiple entries in an XML file and sometimes users would like to obtain all the values of a particular tag. In this case, the selectHash() method is suitable to retrieve all the values of a particular tag and store them into a C# Hashtable. The format of the method is shown as follows.
Hashtable selectHash(String xmlFile, String hashTag)
For example, to retrieve all the values of the VocabularyName tag in the XML file, the code is written as follows.
CSharpLBXMLOperator lbXMLOperator = new CSharpLBXMLOperator();
Hashtable vocabularyNameHash = new Hashtable();
vocabularyNameHash = lbXMLOperator.selectHash("./xmlfile.xml",
"VocabularyName");
Console.WriteLine("vocabularyNameHash = " + vocabularyNameHash);
After running the above code, the following result is displayed.
vocabularyNameHash = {3=CreditRequestNo, 2=OrderedNumber,
1=InputCreditCardNUmber, 0=OrderedNumber}
2.4.5 selectSet()
Similar to the selectHash() method, the selectSet() method also deals with the problem to retrieve all the values of a particular tag in an XML file. The difference between them is that the result of selectSet() is stored into Hashtable in which the same value for different keys does not exist. The format of the method is shown as follows.
Hashtable selectSet(String xmlFile, String setTag)
For example, to do the same searching in Section 2.4.4, the code is written as follows.
CSharpLBXMLOperator lbXMLOperator = new CSharpLBXMLOperator();
Hashtable vocabularyNameSet = new Hashtable();
vocabularyNameSet = lbXMLOperator.selectSet("./xmlfile.xml",
"VocabularyName");
Console.WriteLine("vocabularyNameSet = " + vocabularyNameSet);
After the above searching, the result is displayed as follows.
vocabularyNameSet = [OrderedNumber, InputCreditCardNumber,
CreditRequestNo]
It is noted that OrderedNumber appears two times in the result of selectHash(), but it has only one entry in the result of selectSet() because the result is stored in Hashtable instead of Hashtable.
2.4.6 selectByTagAndWhereForHash()
selectByTagAndWhereForHash() is the integration of the selectByKeyAndWhere() method and the selectHash() method. Because it is required to specify a key tag and its value in the parameters of the method, the number of searching results is smaller than that from selectHash(), i.e., only the values that are related to the key tag are returned into a Hashtable. The format of the method is shown as follows.
Hashtable selectByTagAndWhereForHash(String xmlFile, String keyTag,
String searchTag,
String keyValue)
For example, if users need to search values of the VocabularyName tag, which are related to the BigBug.com value, of the key tag, Organization, the code is written as follows.
CSharpLBXMLOperator lbXMLOperator = new CSharpLBXMLOperator();
Hashtable vocabularyNameHash = new Hashtable();
vocabularyNameHash = lbXMLOperator.selectByTagAndWhereForHash(
"./xmlfile.xml", "Organization",
"VocabularyName",
"BigBug.com");
Console.WriteLine("vocabularyNameHash = " + vocabularyNameHash);
The result of the above code is shown as follows.
vocabularyNameHash = {1=InputCreditCardNumber, 0=OrderedNumber}
2.4.7 selectByTagAndWhereForSet()
Similar to the selectByTagAndWhereForHash() method, selectByTagAndWhereForSet() has the same functionality. The difference is that the searching result is returned into a Hashtable for set instead of a Hashtable. The format of the method is shown as follows.
Hashtable selectByTagAndWhereForSet(String xmlFile, String keyTag,
String searchTag,
String keyValue)
If you are doing the same searching as the Section 2.4.6, the code is written as follows.
CSharpLBXMLOperator lbXMLOperator = new CSharpLBXMLOperator();
Hashtable vocabularyNameSet = new Hashtable();
vocabularyNameSet = lbXMLOperator.selectByTagAndWhereForSet(
"./xmlfile.xml", "Organization", "VocabularyName", "BigBug.com");
Console.WriteLine("vocabularyNameSet = " + vocabularyNameSet);
The result of the above code is displayed as follows.
vocabularyNameSet = [OrderedNumber, InputCreditCardNumber]
2.4.8 selectByMultipleTagsAndWhere()
selectByMultipleTagsAndWhere() is a powerful searching approach. By using this method, users can specify more complex conditions to retrieve a value of a tag than the ones in the above sections. The method is suitable to XML files that have a lot of levels. Usually, when the level of an XML exceeds four, it is possible to consider using the method. The format of the method is shown as follows.
String selectByMultipleTagsAndWhere(String xmlFile,
Hashtable keyTagHash,
Hashtable keyValueHash)
There are two Hashtables in the parameters of the method. The two Hashtables are used to store complex conditions to retrieve a value of a tag. The first one, keyTagHash, is used to store key tags and the second, keyValueHash, is used to store corresponding key values. With those constraints, the method is able to retrieve the value of a particular tag exactly.
To demonstrate the utilization of the method, the XML file is changed 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>
<Contract>
<ContractName>WholesaleContract</ContractName>
<ContractName>CreditCheckingContract</ContractName>
</Contract>
</SAT>
</NewSAT>
<ExistingSAT>
<SAT>
<Organization>BigBug.com</Organization>
<User>guest</User>
<Workflow>Retailer-Wholesaler</Workflow>
<Contract>
<ContractName>CreditCheckingContract</ContractName>
</Contract>
</SAT>
<SAT>
<Organization>BigBug.com</Organization>
<User>customer</User>
<Workflow>Retailer-Wholesaler</Workflow>
<Contract>
<ContractName>CreditCheckingContract</ContractName>
</Contract>
</SAT>
</ExistingSAT>
</RequirementInSAT>
List 16. A more complex XML file for searching example
For example, to search the value of the ContractName tag, which is underlined, all the methods in the above section are not suitable because there is no unique key in the XML file. Because the method provides the parameters to specify multiple key tags and values, it can be used here to deal with this case. The corresponding code is written as follows.
CSharpLBXMLOperator lbXMLOperator = new CSharpLBXMLOperator();
Hashtable keyTagHash = new Hashtable();
Hashtable keyValueHash = new Hashtable();
keyTagHash.put("0", "Organization");
keyTagHash.put("1", "User");
keyTagHash.put("2", "Workflow");
keyTagHash.put("3", "ContractName");
keyValueHash.put("0", "BigBug.com");
keyValueHash.put("1", "customer");
keyValueHash.put("2", "Retailer-Wholesaler");
keyValueHash.put("3", "?");
String contractName =
lbXMLOperator.selectByMultipleTagsAndWhere("./xmlfile.xml",
keyTagHash, keyValueHash);
Console.WriteLine("contractName = " + contractName);
The result of the above code is displayed as follows.
contractName = CreditCheckingContract
2.4.9 selectByMultipleTagsAndWhereForHash()
Although selectByMultipleTagsAndWhere() is able to handle searching complex XML files, there is a big drawback for it. Sometimes, after specifying all the tags and corresponding values, it is possible that the result is not unique as selectByMultipleTagsAndWhereForHash() expects. To handle this problem, selectByMultipleTagsAndWhereForHash() is a good choice because the result is allowed to be not unique and stored into a Hashtable. The format of the method is as follows, which is similar to selectByMultipleTagsAndWhere() and expects return values.
Hashtable selectByMultipleTagsAndWhereForHash(String xmlFile,
Hashtable, keyTagHash, Hashtable keyValueHash)
For example, there is an XML file shown in List 17. If users need to retrieve the value of the ExternalPropertyName tag for PC of the Organization, BigBug.com, none of the above methods can be used here. Multiple tags and corresponding values are required to be specified in this case; meanwhile, the searching results are not unique.
<?xml version="1.0"?>
<!DOCTYPE RequirementInSAT SYSTEM "requirement_in_sat.dtd">
<RequirementInSAT>
<SAT>
<Organization>
<OrganizationName>BigBug.com</OrganizationName>
<External>
<ExternalDetail>
<ExternalName>PC</ExternalName>
<ExternalProperty>
<ExternalPropertyName>InStock</ExternalPropertyName>
<ExternalPropertyValue>9</ExternalPropertyValue>
</ExternalProperty>
<ExternalProperty>
<ExternalPropertyName>MerchandisePrice</ExternalPropertyName>
<ExternalPropertyValue>399</ExternalPropertyValue>
</ExternalProperty>
</ExternalDetail>
<ExternalDetail>
<ExternalName>customer</ExternalName>
<ExternalProperty>
<ExternalPropertyName>UserName</ExternalPropertyName>
<ExternalPropertyValue>greatfree</ExternalPropertyValue>
</ExternalProperty>
<ExternalProperty>
<ExternalPropertyName>Password</ExternalPropertyName>
<ExternalPropertyValue>111111</ExternalPropertyValue>
</ExternalProperty>
<ExternalProperty>
<ExternalPropertyName>CreditCardNumber</ExternalPropertyName>
<ExternalPropertyValue>1234567890</ExternalPropertyValue>
</ExternalProperty>
</ExternalDetail>
</External>
List 17. A complex XML file
The code to complete the above searching is shown as follows.
CSharpLBXMLOperator lbXMLOperator = new CSharpLBXMLOperator();
Hashtable keyTagHash = new Hashtable();
Hashtable keyValueHash = new Hashtable();
keyTagHash.put("0", "OrganizationName");
keyTagHash.put("1", "ExternalName");
keyTagHash.put("2", "ExternalPropertyName");
keyValueHash.put("0", "BigBug.com");
keyValueHash.put("1", "PC");
keyValueHash.put("2", "?");
Hashtable externalPropertyNameHash = new Hashtable();
externalPropertyNameHash =
lbXMLOperator.getValueByMultipleTagsAndWhereForHash("./xmlfile.xml",
keyTagHash, keyValueHash);
Console.WriteLine("externalPropertyNameHash = " +
externalPropertyNameHash);
After running the code, the result is displayed as follows.
ExternalPropertyNameHash = {1=MerchandisePrice, 0=InStock}
2.4.10 selectByMultipleTagsAndWhereForSet()
selectByMultipleTagsAndWhereForSet() is similar to selectByMultipleTagsAndWhereForHash() expect for the type of return values. By using selectByMultipleTagsAndWhereForSet(), the searching result is stored into a Hashtable for the set instead of a Hashtable. Because those two methods are almost the same, the example for the method is omitted. The format of the method is shown as follows.
Hashtable selectByMultipleTagsAndWhereForSet(String xmlFile,
Hashtable keyTagHash, Hashtable keyValueHash)
Appendix
- Tool: LBXML Operator
- DLL: com.lblabs.xmltool.dll, com.lblabs.tools.csharp.dll
- Class: CSharpLBXMLOperator
- Constructor: Summary CSharpLBXMLOperator()
Method Summary
| bool |
insertNodeByParentTag(String xmlFile, String parentTag, String updateTag, String updateValue)
- xmlFile: the XML file to be inserted
- parentTag: the parentTag of the tag to be inserted
- updateTag: the tag to be inserted
- updateValue: the value of the tag to be inserted
|
| bool |
insertNodeParentTagAndParentSibling(String xmlFile, String parentTag, String parentSiblingTag, String parentSiblingValue, String updateTag, String updateValue)
- xmlFile: the XML file to be inserted
- parentTag: the parentTag of the tag to be inserted
- parentSiblingTag: the parent sibling tag of the tag to be inserted
- parentSiblingValue: the value of the parent sibling tag of the tag to be inserted
- updateTag: the tag to be inserted
- updateValue: the value of the tag to be inserted
|
| bool |
removeNodeByTagValue(String xmlFile, String tag, String value)
- xmlFile: the XML file to be removed
- tag: the tag to be removed
- value: the value of the tag to be removed
|
| bool |
changeByValue(String xmlFile, String oldValue, String newValue)
- xmlFile: the XML file to be changed
- oldValue: the value to be modified
- newValue: the new value after modification
|
| bool |
changeByTag(String xmlFile, String tag, String newValue)
- xmlFile: the XML file to be changed
- tag: the tag to be modified
- newValue: the new value after modification
|
| bool |
changeByTagNewValue(String xmlFile, String tag, String oldValue, String newValue)
- xmlFile: the XML file to be changed
- tag: the tag to be modified
- oldValue: the value to be modified
- newValue: the new value after modification
|
| bool |
changeByNoTagNewValue(String xmlFile, String tag, int no, String newValue)
- xmlFile: the XML file to be changed
- tag: the tag to be modified
- no: the sequential number of the tag to be modified
- newValue: the new value after modification
|
| bool |
hangeBySiblingTagNewValue(String xmlFile, String siblingTag, String siblingValue, String newValue)
- xmlFile: the XML file to be changed
- siblingTag: the sibling tag of the tag to be modified
- siblingValue: t he sibling value of sibling tag of the tag to be modified
- newValue: the new value after modification
|
| bool |
changeBySiblingTagUpdateTagNewValue(String xmlFile, String siblingTag, String siblingValue, String updateTag, String newValue)
- xmlFile: the XML file to be changed
- siblingTag: the sibling tag of the tag to be modified
- updateTag: the tag to be modified
- newValue: the new value after modification
|
| bool |
changeByKeyTagKeyValueUpdateTagNewValue(String xmlFile, String keyTag, String keyValue, String updateTag, String newValue)
- xmlFile: the XML file to be changed
- keyTag: the key tag of the XML structure the tag to be modified locates
- keyValue: the value of the key tag
- updateTag: the tag to be modified
- newValue: the new value after modification
|
| bool |
changeByKeyTagKeyValueSiblingTagUpdateTagNewValue(String xmlFile, String keyTag, String keyValue, String siblingTag, String siblingValue, String updateTag, String newValue)
- xmlFile: the XML file to be changed
- keyTag: the key tag of the XML structure the tag to be modified locates
- keyValue: the value of the key tag
- siblingTag: the sibling tag of the tag to be modified
- siblingValue: the sibling value of the sibling tag
- updateTag: the tag to be modified
- newValue: the new value after modification
|
| bool |
changeByMultipleTagsAndWhere(String xmlFile, Hashtable keyTagHash, Hashtable keyValueHash)
- xmlFile: the XML file to be changed
- keyTagHash: the multiple key tags
- keyValueHash: the values of the multiple key tags
|
| String |
selectByKeyTag(String xmlFile, String keyTag)
- xmlFile: the XML file to be searched
- keyTag: the tag to be searched
|
| String |
selectByTagAndWhere(String xmlFile, String keyTag, String searchTag, String keyValue)
- xmlFile: the XML file to be searched
- keyTag: the key tag of the XML structure the tag to be searched locates
- searchTag: the tag to be searched
- keyValue: the value of the key tag
|
| String |
selectByBelowTagAndWhere(String xmlFile, String belowKeyTag, String searchTag, String belowKeyValue)
- xmlFile: the XML file to be searched
- belowKeyTag: the key tag of the XML structure the tag to be searched locates; the key tag is below the tag to be searched
- searchTag: the tag to be searched
- belowKeyValue: the value of the belowKeyTag
|
| Hashtable |
selectHash(String xmlFile, String hashTag)
- xmlFile: the XML file to be searched
- hashTag: the tag to be searched
|
| Hashtable |
selectSet(String xmlFile, String setTag)
- xmlFile: the XML file to be searched
- setTag: the tag to be searched
|
| Hashtable |
selectByTagAndWhereForHash(String xmlFile, String keyTag, String searchTag, String keyValue)
- xmlFile: the XML file to be searched
- keyTag: the key tag of the XML structure the tag to be searched locates
- searchTag: the tag to be searched
- keyValue: the value of the key tag
|
| Hashtable |
selectByTagAndWhereForSet(String xmlFile, String keyTag, String searchTag, String keyValue)
- xmlFile: the XML file to be searched
- keyTag: the key tag of the XML structure the tag to be searched locates
- searchTag: the tag to be searched
- keyValue: the value of the key tag
|
| String |
selectByMultipleTagsAndWhere(String xmlFile, Hashtable keyHash, Hashtable keyValueHash)
- xmlFile: the XML file to be searched
- keyHash: the multiple key tags of the XML structure the tag to be seached locates
- keyValueHash: the multiple key values of the corresponding key tags
|
| Hashtable |
selectByMultipleTagsAndWhereForHash(String xmlFile, Hashtable keyHash, Hashtable keyValueHash)
- xmlFile: the XML file to be searched
- keyHash: the multiple key tags of the XML structure the tag to be seached locates
- keyValueHash: the multiple key values of the corresponding key tags
|
| Hashtable |
selectByMultipleTagsAndWhereForSet(String xmlFile, Hashtable keyHash, Hashtable keyValueHash)
- xmlFile: the XML file to be searched
- keyHash: the multiple key tags of the XML structure the tag to be seached locates
- keyValueHash: the multiple key values of the corresponding key tags
|
References
- XPATH: http://www.w3.org/TR/xpath
- GMD-IPSI XQL: http://xml.darmstadt.gmd.de/xql/
- XSet: http://www.cs.berkeley.edu/~ravenben/xset/
- Fxgrep: http://www.informatik.uni-trier.de/~aberlea/Fxgrep/
- Quip: http://developer.softwareag.com/tamino/quip/
- XML:QL: http://theoryx5.uwinnipeg.ca/mod_perl/cpan-search?dist=XML-QL
Comments
There are no comments yet. Be the first to comment!