In my application, I need to extract the data from HBase and convert the same into XML. Data is perfectly fetched from HBase but when we convert it into XML, all the special characters like "&" are getting converted to "&". Below is the code that I have written:
public static String getDataFromHBase(TagReplaceTO tagReplaceTO, int organizationId, int tenantId, String outBoundName, String hbaseServer, String hbasePort)
throws Exception
{
Map<String, List<IntegrationTagTO>> tagMap = tagReplaceTO.getTagMap();
String headTag = tagReplaceTO.getTagName();
String headTagReplace = getSystemHeadTagName(headTag, tagMap);
String[] rootSystem = headTagReplace.split(":");
String columnFamily = null;
int count = 0;
String columnNameTemp = null;
String columnValue = null;
String colVal[] = null;
try {
//fetch data from Hbase
Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", hbaseServer);
config.set("hbase.zookeeper.property.clientPort", hbasePort);
HTable table = new HTable(config, rootSystem[0]);
SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes(rootSystem[1]), Bytes.toBytes("organizationid"), CompareFilter.CompareOp.EQUAL,
new BinaryComparator(Bytes.toBytes(String.valueOf(organizationId))));
Scan scan = new Scan();
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
String collon = ":";
String headNameSpace = rootSystem[0] + collon + rootSystem[1];
String rootColumn = getClientFirstColumnForFirstTime(headNameSpace, tagMap);
String[] rootColumns = rootColumn.split(":");
headNameSpace = headNameSpace + collon + rootColumns[2];
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element root = document.createElement(outBoundName);
document.appendChild(root);
Element subRoot = document.createElement(rootColumns[0]);
root.appendChild(subRoot);
for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
Element rowElement = document.createElement(rootColumns[1]);
subRoot.appendChild(rowElement);
for (KeyValue keyValue : rr.list()) {
columnFamily = Bytes.toString(keyValue.getFamily());
columnNameTemp = Bytes.toString(keyValue.getQualifier());
columnValue = Bytes.toString(keyValue.getValue());
columnValue = columnValue.trim();
//convert Hbase data into XML
String columnNameWithNameSpace = headNameSpace + collon + columnNameTemp.trim();
String columnName = getClientFirstColumnForFirstTime(columnNameWithNameSpace, tagMap);
if (columnName != null) {
String splitColumn[] = columnName.split(":");
if (!(columnValue.equals("\\n") || columnValue.isEmpty())) {
String split[] = columnValue.split("\\$");
if (split.length == 1) {
Element leafElement = document.createElement(splitColumn[1]);
if (split.length == 1) {
leafElement.appendChild(document.createTextNode(split[0]));
} else {
leafElement.appendChild(document.createTextNode(""));
}
rowElement.appendChild(leafElement);
} else {
Element subRowElement = document.createElement(splitColumn[0]);
rowElement.appendChild(subRowElement);
for (int i = 1; i < split.length; i++) {
Element subSubRowElement = document.createElement(splitColumn[1]);
String splinIn[] = split[i].split("#");
for (String str : splinIn) {
colVal = str.split(":");
String leafColumnWithNameSpace = headNameSpace + collon + columnNameTemp + collon + splitColumn[2] + collon + colVal[0].trim();
String columnLeafVal = getClientLeafColumn(leafColumnWithNameSpace, tagMap);
if (columnLeafVal != null) {
Element leafElement = document.createElement(columnLeafVal);
if (colVal.length == 2) {
leafElement.appendChild(document.createTextNode(colVal[1]));
} else {
leafElement.appendChild(document.createTextNode(""));
}
subSubRowElement.appendChild(leafElement);
}
}
subRowElement.appendChild(subSubRowElement);
}
}
} else {
String[] splitCol = columnName.split(":");
rowElement.appendChild(document.createElement(splitCol[0]));
}
}
}
}
table.close();
DOMSource domSource = new DOMSource(document);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
return writer.toString();
} catch (Exception e) {
throw e;
}
}
What change do I need to make to overcome this issue?