1
在我的應用程序中,我需要從HBase中提取數據並將其轉換爲XML。數據完全取自HBase,但當我們將其轉換爲XML時,所有特殊字符(如「&」)正在轉換爲「& amp」。以下是我寫的代碼:特殊字符在將數據轉換爲xml時被替換
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;
}
}
我需要做些什麼改變來克服這個問題?
更多信息快速搜索之後,似乎一個'&'是在XML文件非法,因此被轉換成'&amp'。 [類似問題的答案](https://stackoverflow.com/a/17424187/6707985)沒有Java的直接規範。 – geisterfurz007
這不是一個錯誤,它是一個功能。 – Aaron
谷歌「html轉義」 – vikingsteve