【问题标题】:Special Characters getting replaced while converting data into xml将数据转换为 xml 时特殊字符被替换
【发布时间】:2017-10-11 08:39:53
【问题描述】:

在我的应用程序中,我需要从 HBase 中提取数据并将其转换为 XML。数据完美地从 HBase 中获取,但是当我们将其转换为 XML 时,所有像“&”这样的特殊字符都被转换为“&amp”。下面是我写的代码:

    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;
        }
    }

我需要做出什么改变来克服这个问题?

【问题讨论】:

  • 快速搜索后,似乎单个&amp; 在XML 文件中是非法的,因此被转换为&amp;ampSimilar questions answer 没有对 Java 的直接规范。
  • 这不是错误,而是一项功能。
  • google "html 转义"

标签: java xml


【解决方案1】:

那是因为某些字符在 XML 中是不允许的,所以用转义实体代替。

如果您希望 XML 中的原始数据没有转义实体,则需要将其包装在 CDATA 块中。

有关 w3schools.org 的更多信息

【讨论】:

    猜你喜欢
    • 2019-11-04
    • 2019-05-09
    • 2011-06-05
    • 1970-01-01
    • 2012-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-08
    相关资源
    最近更新 更多