2017-02-09 5 views
0

にHTML進コードまたはUnicode進コードに絵文字を変換します。 例:は、私は絵文字のHTMLコードまたはJavaを使用して16進コードのファイルに絵文字内容のテキストファイルを変換しようとしていたJava

I/P:P/oを期待<div id="thread" style="white-space: pre-wrap;"><div>⚽️

:上記で<div id="thread" style="white-space: pre-wrap;"><div>😀😀😃🍎🍏⚽️🏀

が出''は、対応するHTMLエンティティコードにするHTMLエンティティコードの'& # 128512;'

詳細を変更を取得する必要があり置きますと進コードをここに与えられます。その http://character-code.com/emoticons-html-codes.php

サンプルコードを私が試し以下の通りです:

try { 
      File file = new File("/inFile.txt"); 
      str = FileUtils.readFileToString(file, "ISO-8859-1"); 
      System.out.println(new String(str.getBytes(), "UTF-8")); 
      String results = StringEscapeUtils.escapeHtml4(str); 
      System.out.println(results); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
+1

だから、あなたが何かをするコードを持っている、あなたは私たちのコードを示していないし、その後のコードが動作しない理由を尋ねますか? *本当に?!?!?* – Andreas

+0

は、私が試したサンプルコードを追加しました。 –

+1

あなたは、ファイルが 'ISO-8859-1'エンコーディングを使用していますか?それは...そうは思わない。 – dnault

答えて

0
I got the work around : 
public static void htmlDecimalCodeGenerator() { 

    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); 

    domFactory.setValidating(false); 

    // File inputFile = new File("/inputFile.xml"); 
    File inputFile = new File("/inputFile.xml"); 



    try { 

    FileOutputStream fop = null; 

    File OutFile = new File("/outputFile.xml"); 

    fop = new FileOutputStream(OutFile); 



    DocumentBuilder builder = domFactory.newDocumentBuilder(); 

    Document doc = builder.parse(inputFile); 



    TransformerFactory tf = TransformerFactory.newInstance(); 

    Transformer transformer = tf.newTransformer(); 



    /* 
    no value of OMIT_XML_DECLARATION will add following xml declaration in the beginning of the file. 
    <?xml version='1.0' encoding='UTF-32'?> 
    */ 
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 



    /* 

    When the output method is "xml", the version value specifies the 
    version of XML to be used for outputting the result tree. The default 
    value for the xml output method is 1.0. When the output method is 
    "html", the version value indicates the version of the HTML. 
    The default value for the xml output method is 4.0, which specifies 
    that the result should be output as HTML conforming to the HTML 4.0 
    Recommendation [HTML]. If the output method is "text", the version 
    property is ignored 
    */ 
    transformer.setOutputProperty(OutputKeys.METHOD, "xml"); 



    /* 
    Indent-- specifies whether the Transformer may 
    add additional whitespace when outputting the result tree; the value 
    must be yes or no. 
    */ 
    transformer.setOutputProperty(OutputKeys.INDENT, "no"); 





    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); 

    // transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); 

    transformer.transform(new DOMSource(doc), 

    new StreamResult(new OutputStreamWriter(System.out, "UTF-8"))); 

    // new StreamResult(new OutputStreamWriter(fop, "UTF-8"))); 


    } catch (Exception e) { 

    e.printStackTrace(); 

    } 

} 

} 
関連する問題