Query Cell Areas Mapped to XML Map Path using Worksheet.XmlMapQuery method

Possible Usage Scenarios

You can query cell areas mapped to XML map path with Aspose.Cells using the Worksheet.xmlMapQuery() method. If the path exists, it will return the list of cell areas related to that path inside the XML map. The first parameter of Worksheet.xmlMapQuery() method specifies the XML element path and the second parameter specifies an XML map you want to query.

Query Cell Areas Mapped to XML Map Path using Worksheet.XmlMapQuery method

The following screenshot shows the Microsoft Excel displaying XML Map inside the sample Excel file used in the code. The code queries the XML map two times and prints the list of cell areas returned by the Worksheet.xmlMapQuery() method on the console as shown below.

todo:image_alt_text

Sample Code

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Load sample Excel file having Xml Map
Workbook wb = new Workbook("sampleXmlMapQuery.xlsx");
//Access first XML Map
XmlMap xmap = wb.getWorksheets().getXmlMaps().get(0);
//Access first worksheet
Worksheet ws = wb.getWorksheets().get(0);
//Query Xml Map from Path - /MiscData
System.out.println("Query Xml Map from Path - /MiscData");
ArrayList ret = ws.xmlMapQuery("/MiscData", xmap);
//Print returned ArrayList values
for (int i = 0; i < ret.size(); i++)
{
System.out.println(ret.get(i));
}
System.out.println("");
//Query Xml Map from Path - /MiscData/row/Color
System.out.println("Query Xml Map from Path - /MiscData/row/Color");
ret = ws.xmlMapQuery("/MiscData/row/Color", xmap);
//Print returned ArrayList values
for (int i = 0; i < ret.size(); i++)
{
System.out.println(ret.get(i));
}

Console Output

Query Xml Map from Path - /MiscData

Aspose.Cells.CellArea(A1:A8)[0,0,7,0]

Aspose.Cells.CellArea(B1:B8)[0,1,7,1]

Aspose.Cells.CellArea(C1:C8)[0,2,7,2]

Aspose.Cells.CellArea(D1:D8)[0,3,7,3]

Aspose.Cells.CellArea(E1:E8)[0,4,7,4]

Query Xml Map from Path - /MiscData/row/Color

Aspose.Cells.CellArea(D1:D8)[0,3,7,3]

Get XML path from List Object/Table

XML data can be imported to worksheets. Sometimes XML path is required from the ListObjects of the worksheet. This feature is available in Excel by using an expression like Sheet1.ListObjects(1).XmlMap.DataBinding. The same feature is available in Aspose.Cells by calling ListObject.getXmlMap().getDataBinding().getUrl().  Following example demonstrates this feature. Template file and other source files can be downloaded from the following links:

  1. XMLData.xlsx
  2. CountryList.xml
  3. FoodList.xml

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Load XLSX file containing data from XML file
Workbook workbook = new Workbook("XML Data.xlsx");
// Access the first worksheet
Worksheet ws = workbook.getWorksheets().get(0);
// Access ListObject from the first sheet
ListObject listObject = ws.getListObjects().get(0);
// Get the url of the list object's xml map data binding
String url = listObject.getXmlMap().getDataBinding().getUrl();
// Display XML file name
System.out.println(url);