2016-04-22 72 views
0

我有以下字符串:工具字符串轉換爲XML安全的字符串

Here's the thing. You said a "mountain lion is a lion." Is it in the same family? Yes. No one's arguing that. As someone who is a scientist who studies lions, I am telling you, specifically, in science, no one calls mountain lions "lions". If you want to be "specific" like you said, then you shouldn't either. They're not the same thing. If you're saying lion family" you're referring to the taxonomic grouping of Felidae, which includes things from house cats to ocelots to tigers. So your reasoning for calling a mountain lion a lion is because random people "call the big ones lions?" Let's get panthers and leopards in there, then, too. Also, calling someone a human or an ape? It's not one or the other, that's not how taxonomy works. They're both. A mountain lion is a mountain lion and a member of the lion family. But that's not what you said. You said a mountain lion is a lion, which is not true unless you're okay with calling all members of the lion family lions, which means you'd call house cats, tigers, and other cats lions, too. Which you said you don't. It's okay to just admit you're wrong, you know?

,我想轉換成一個正確格式化字符串把我的strings.xml文件。

我使用以下工具嘗試:

http://coderstoolbox.net/string/#!encoding=xml&action=encode&charset=none http://www.freeformatter.com/xml-escape.html#ad-output

但每次我建項目中,我得到了以下錯誤時間:

Error:(39, 5) Apostrophe not preceded by \ (in Here's the thing. You said a "mountain lion is a lion."

我能做些什麼?

另外奇怪的是,字符串中的所有撇號已被替換爲 ' 所以我不知道爲什麼我仍然得到該錯誤。當我手動將\放在每個'前面時都很好,但這非常不方便。我想自動化這個過程。

回答

1

從您的原始字符串開始。將所有'替換爲\'。將所有"替換爲\"。用它作爲你的字符串資源的值。

你風與:

<resources> 
    <string name="whatevs">Here\'s the thing. You said a \"mountain lion is a lion.\" Is it in the same family? Yes. No one\'s arguing that. As someone who is a scientist who studies lions, I am telling you, specifically, in science, no one calls mountain lions \"lions\". If you want to be \"specific\" like you said, then you shouldn\'t either. They\'re not the same thing. If you\'re saying lion family\" you\'re referring to the taxonomic grouping of Felidae, which includes things from house cats to ocelots to tigers. So your reasoning for calling a mountain lion a lion is because random people \"call the big ones lions?\" Let\'s get panthers and leopards in there, then, too. Also, calling someone a human or an ape? It\'s not one or the other, that\'s not how taxonomy works. They\'re both. A mountain lion is a mountain lion and a member of the lion family. But that\'s not what you said. You said a mountain lion is a lion, which is not true unless you\'re okay with calling all members of the lion family lions, which means you\'d call house cats, tigers, and other cats lions, too. Which you said you don\'t. It\'s okay to just admit you\'re wrong, you know?</string> 
</resources> 
+0

問題是我想自動化這個過程 – Roymunson

+0

@Roymunson:字符串替換已經存在,因爲COBOL,如果我沒記錯的話。 uage,高於彙編程序,並不提供Java的replaceAll(「'」,「」')「等等。 – CommonsWare

+1

我正在尋找一個可以爲我做的在線工具。但由於編程非常簡單,所以我會自己做出快速解決方案。 – Roymunson

0

CommonsWare提供把一個斜線每`和」之前的答案

我做了一個簡單的Java程序,將做到這從一個文本文件:

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.nio.charset.Charset; 
import java.util.Scanner; 
import java.util.stream.Collectors; 

public class Main { 

    public static void main(String[] args) throws FileNotFoundException, IOException { 
     //Put the string you want formatted here. 
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 

     String input = "EMPTY STRING"; 
     File 



inputFile = new File("[Path to File]"); 

     input = stringToFile(inputFile); 

     input = input.replace("\'", "\\\'"); 
     input = input.replace("\"", "\\\""); 

     System.out.println("Output:"); 
     System.out.println(input); 


    } 




public static String stringToFile(File filename) throws FileNotFoundException, IOException { 
     try (BufferedReader in = new BufferedReader(new FileReader(filename))) 
     { 
      return in.lines().collect(Collectors.joining("\n")); 
     } 
} 

}