Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将字符串从excel文件转换为.xml文件时出错_Java_Xml_Xlsx - Fatal编程技术网

Java 将字符串从excel文件转换为.xml文件时出错

Java 将字符串从excel文件转换为.xml文件时出错,java,xml,xlsx,Java,Xml,Xlsx,我从.xlsx文件中提取了一些字符串(字符串是简单字符)。然后我尝试将这些字符串放入.xml文件中。但不幸的是,当我将这些字符串放入'createElement(StringVariableHere)'方法中时,我得到了以下错误:“org.w3c.dom.domeException:INVALID_CHARACTER_ERR:指定了无效或非法的XML字符。” 我通过以下方式获取字符串值: switch (tempCell.getCellType()) { case Cell.CELL_T

我从.xlsx文件中提取了一些字符串(字符串是简单字符)。然后我尝试将这些字符串放入.xml文件中。但不幸的是,当我将这些字符串放入'createElement(StringVariableHere)'方法中时,我得到了以下错误:“org.w3c.dom.domeException:INVALID_CHARACTER_ERR:指定了无效或非法的XML字符。”

我通过以下方式获取字符串值:

switch (tempCell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
    String tempColValue = tempCell.getStringCellValue();
}
这是我尝试添加字符串值的那一行,它给出了错误

Element titleChild = doc.createElement(StringVariableHere);
我甚至尝试使用在线找到的以下方法清理字符串:

public String stripNonValidXMLCharacters(String in) {
    StringBuffer out = new StringBuffer(); // Used to hold the output.
    char current; // Used to reference the current character.

    if (in == null || ("".equals(in))) return ""; // vacancy test.
    for (int i = 0; i < in.length(); i++) {
        current = in.charAt(i); // NOTE: No IndexOutOfBoundsException caught here; it should not happen.
        if ((current == 0x9) ||
            (current == 0xA) ||
            (current == 0xD) ||
            ((current >= 0x20) && (current <= 0xD7FF)) ||
            ((current >= 0xE000) && (current <= 0xFFFD)) ||
            ((current >= 0x10000) && (current <= 0x10FFFF)))
            out.append(current);
    }
    return out.toString();
}

非常感谢大家抽出时间。Stefanos.

您是否尝试查看java中的字符串?是否将其打印到控制台等

这让我想起了我在解析office文档时遇到的类似问题。解析器(ApachePOI)有时会给出破坏xml的unicode无效字符(例如换行符)

我不知道您使用的是什么解析器,但在尝试填充xml之前,您可能必须清理字符串

在添加详细信息后进行编辑

你想写什么样的xml?你能举个例子吗? createElement(StringVariableHere)表示您尝试创建名为StringVariableHere的元素。即

<StringVariableHere>there could be something here</StringVariableHere>
这里可能有东西
不是

StringVariableHere

您有什么问题吗?谢谢。是的,我已经把它打印到控制台上了,它工作得很好。我也使用相同的解析器(ApachePOI)。为了清除xml字符串,我在上面发布了一个我在网上找到的方法……我编辑了我的答案。我认为是xml部分出错了…感谢您的帮助!我试图将字符串作为标记内容中的值而不是标记名写入xml。它起作用了。但最后有以下几个字符 ;你知道它们是什么意思以及如何移除它们吗?请看这里了解它是什么。我不知道如何移除它们。。。
<StringVariableHere>there could be something here</StringVariableHere>
<aRandomTag>StringVariableHere</aRandomTag>