将字符串转换为XML安全字符串

时间:2016-04-22 16:29:19

标签: android xml string

我有以下字符串:

  

这就是事情。你说山狮是狮子。"   它在同一个家庭吗?是。没有人在争论。   作为研究狮子的科学家,我告诉你,特别是在科学方面,没有人称之为狮子狮子"狮子"。如果你想成为特定的"就像你说的,那你也不应该。他们不是一回事。   如果你说狮子家族"你指的是猫科的分类群,包括从家猫到猫眼到老虎的东西。   所以你把山狮称为狮子的理由是因为随机的人会把大狮子称为狮子?"那么,让我们在那里得到黑豹和豹子。   还有,称某人为人或猿?它不是一个或另一个,不是分类法如何运作。他们俩都是。山狮是一头山狮,是狮子家族的一员。但那不是你说的。你说山狮是狮子,除非你打电话给狮子家狮的所有成员,这也不是真的,这意味着你也可以称之为家猫,老虎和其他猫狮。你说的不是你。   你知道吗?只是承认你错了,这没关系吗?

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

我尝试使用以下工具:

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

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

  

错误:(39,5)Apostrophe之前没有\(在这里' s。你说a"山狮是狮子。"

我该怎么办?

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

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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"));
        }
} 

}
相关问题