-1

I am taking some strings from a .xlsx file (the strings are simple characters). Then I am trying to put those strings in an .xml file. But unfortunately, when I put those strings in the ‘createElement(StringVariableHere)’ method, I get the following error: "org.w3c.dom.DOMException: INVALID_CHARACTER_ERR: An invalid or illegal XML character is specified."

I get the string values in this way:

switch (tempCell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
    String tempColValue = tempCell.getStringCellValue();
}

And this is the line where i try to add my string values and it gives me the error.

Element titleChild = doc.createElement(StringVariableHere);

I even tried to clean the string using the following method i found online:

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

Also i am using the following to check if it is valid or not, and when i add my string it returns false:

XMLChar.isValidName(StringVariableHere)

Thank you all so much for your time. Stefanos.

Stefanos
  • 39
  • 1
  • 5

1 Answers1

1

did you try looking at the string you get in java? printing it to the console or such?

It reminds me of similar issues i had parsing office document. The parser (apache POI) was sometimes giving unicode invalid characters which were breaking the xml (one example was with line breaks).

I don't know what parser you are using but you might have to clean your string before trying to fill your xml.

Edit after the added details.

What kind of xml are you trying to write? can you provide an example? doc.createElement(StringVariableHere) means you try to create an element named StringVariableHere. I.E.

<StringVariableHere>there could be something here</StringVariableHere>

not

<aRandomTag>StringVariableHere</aRandomTag>
florent
  • 801
  • 4
  • 17
  • Thank you. Yes, i have printed it to the console and it's working fine. Also i am using the same parser(apache POI). I posted above a method that I have found online in order to clean the xml string… – Stefanos Dec 04 '12 at 22:45
  • i edited my answer. I think it's the xml part which is wrong... – florent Dec 04 '12 at 23:06
  • Thanks for your help! I tried to write the string to the xml as the value in the content of the tag, and not as a tag name. It's working. But at the end there are the following characters do you know what do they mean and how to remove them? – Stefanos Dec 04 '12 at 23:29
  • 1
    see here http://stackoverflow.com/questions/1459170/what-is-13 for what it is. I don't know how to remove them... – florent Dec 05 '12 at 08:39