【问题标题】:Tool to convert string into XML safe string将字符串转换为 XML 安全字符串的工具
【发布时间】:2016-04-22 16:29:18
【问题描述】:

我有以下字符串:

事情就是这样。你说“山狮是狮子”。 是在同一个家庭吗?是的。没有人这么争论。 作为一个研究狮子的科学家,我告诉你,具体来说,在科学上,没有人称山狮为“狮子”。如果你想像你说的那样“具体”,那么你也不应该。他们不是一回事。 如果您说的是狮子科,那么您指的是猫科动物的分类群,其中包括从家猫到豹猫再到老虎的事物。 所以你称山狮为狮子的理由是因为随机的人“称大狮子为狮子?”让我们把黑豹和豹子也放在那里。 另外,称某人为人还是猿?这不是一个或另一个,这不是分类学的工作方式。他们都是。山狮是山狮,是狮子家族的一员。但这不是你说的。你说山狮是狮子,这不是真的,除非你可以称狮子家族的所有成员为狮子,这意味着你也会称家猫、老虎和其他猫科动物为狮子。你说你没有。 承认自己错了也没关系,你知道吗?

我想将其转换为格式正确的字符串以放入我的 strings.xml 文件中。

我尝试使用以下工具:

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

但每次我构建项目时都会出现以下错误:

错误:(39, 5) 撇号前面没有 \ (事情是这样的。你说“山狮是狮子”。

我能做什么?

另外奇怪的是字符串中的所有撇号都被替换为 ' 所以我不确定为什么我仍然会收到这个错误。当我手动将\ 放在每个' 前面时,这很好,但这很不方便。我想自动化这个过程。

【问题讨论】:

    标签: android xml string


    【解决方案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>
    

    【讨论】:

    • 问题是我想自动化这个过程。
    • @Roymunson:如果我没记错的话,字符串替换自 COBOL 以来就一直存在。请说出一种现代编程语言,它比汇编程序更高,它不提供与 Java 的 replaceAll("'","\'") 等价的功能。
    • 我一直在寻找可以为我做这件事的在线工具。但由于它很容易编程,我会自己快速解决。
    • 记事本就可以了。查找 |替换,用\'替换'
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2010-12-06
      • 2021-03-30
      • 1970-01-01
      • 2012-10-26
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 2018-08-29
      相关资源
      最近更新 更多