Ich habe ein Problem:
Ich habe eine xml-Datei in der z.B. folgendes steht:
umlaute & #228;& #246;& #252;& #223;.doc
Die echte Datei, die ich öffnen möchte heißt aber äöüß.doc
Gibt es eine Möglichkeit die Umformung aller Asciitabelleninhalte in Umlaute und andere UTF-8 Zeichen in einem Schritt zu machen?
wie verarbeitest du denn dein XML-Dokument? Wenn du bspw. JDOM verwendest, dann kannst du mit getContent()/getText() den Inhalt eines Elements erhalten. Der String ist dann automatisch mit Umlauten.
Ich hab mir für sowas eine Methode geschrieben:
Code:
/**
* This function retrieves an element of a xml document at a given
* position. used for other methods like getAuthor or
* getKeyword. The position is a value from 1 to (size of xml file) - in contrary
* to usual array handling where the range is from 0 to (size-1).
*
* @param doc (the xm document where to look for elements)
* @param pos (the position of the element
* @return the element if a match was found, otherwise null
*/
private Element retrieveElement(Document doc, int pos) throws IllegalStateException, IndexOutOfBoundsException {
// create a list of all author elements from the author xml file
try {
List<?> elementList = doc.getRootElement().getContent();
// and return the requestet Element
try {
return (Element) elementList.get(pos-1);
}
catch (IndexOutOfBoundsException e) {
return null;
}
}
catch (IllegalStateException e) {
return null;
}
}
Und den Inhalt kriegt man dann bspw. mit
Code:
/**
* This methods returns the keyword of a given position in the keyword datafile
*
* @param pos (a valid position of an element)
* @return the keyword string
*/
public String getKeyword(int pos) {
// retrieve the keyword element
Element keyword = retrieveElement(keywordFile, pos);
// return the matching string value of the keyword element
String retval;
if (null==keyword) retval = "";
else retval = keyword.getText();
return retval;
}
Und wie wäre es wenn du dir eine Hashmap erstellst in der du ein Codiertes zeichen und ein Enkodiertes mapst?
Dann kannst du bei deinem Durchlauf dir Hashmap nach dem Codierten Zeichen durchsuchen und erhälst als Wert das encodierte... musst dir halt einmal die Mühe machen alle ASCII Codes in eine HashMap zuschreiben...
Wenn du dir den Aufwand gemacht hast kannst du es ja hier Posten kann bestimmt irgendwer mal wieder brauchen