Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从大字符串中获取特定子字符串 约翰 2000_Java_Xml_Regex_Substring - Fatal编程技术网

Java 从大字符串中获取特定子字符串 约翰 2000

Java 从大字符串中获取特定子字符串 约翰 2000,java,xml,regex,substring,Java,Xml,Regex,Substring,我将以字符串的形式获取此xml。我需要从字符串生成一个xml文件,并需要使用名称标记对生成的xml文件命名。例如:Jhon.xml。请提供一些指针,以便在java中不使用解析器也可以这样做。使用字符串子字符串或正则表达式正在解析该文件。我想你的意思是你不想分析每一个细节 如果您知道每个元素本身都在一条线上,那么可以使用以下方法 <emp> <name>Jhon</name> <sal>2000</sal> </emp> B

我将以字符串的形式获取此xml。我需要从字符串生成一个xml文件,并需要使用名称标记对生成的xml文件命名。例如:Jhon.xml。请提供一些指针,以便在java中不使用解析器也可以这样做。

使用字符串子字符串或正则表达式正在解析该文件。我想你的意思是你不想分析每一个细节

如果您知道每个元素本身都在一条线上,那么可以使用以下方法

<emp>
<name>Jhon</name>
<sal>2000</sal>
</emp>
BufferedReader br=
弦线;
而((line=br.readLine())!=null){
String[]parts=line.split(“[]”);
字符串标记=部件[1];
字符串值=零件[2];
如果(“名称”。等于(标记)){
}else if(“ruleId”.equals(标记)){
}else if(“ruleVersion”.equals(标记)){
}
}

尽管在xml上使用正则表达式时要小心。。。这将在一行中完成:

BufferedReader br = 
String line;
while((line = br.readLine()) != null) {
    String[] parts = line.split("[<>]");
    String tag = parts[1];
    String value = parts[2];
    if ("name".equals(tag)) {

    } else if ("ruleId".equals(tag)) {

    } else if ("ruleVersion".equals(tag)) {

    }
}
输出:

public static void main(String[] args) throws Exception
{
    String input = "<name>remove use case</name>\n    <ruleId>2161</ruleId>\n    <ruleVersion>0.0.1</ruleVersion>\n    <ruleStatus>New</ruleStatus>\n    <nuggetId>489505737</nuggetId>\n    <icVersionId>50449</icVersionId>\n    <rlVersion>1.0</rlVersion>\n    <modelVersion>1.0</modelVersion>\n    <attributes>\n        <attribute>\n            <attributeName/>\n            <value/>\n        </attribute>\n    </attributes>\n    <notes></notes>";
    String filename = input.replaceAll("(?s).*<name>(.*)</name>.*<ruleId>(.*)</ruleId>.*<ruleVersion>(.*)</ruleVersion>.*", "$1_$2_$3.xml");
    System.out.println(filename);
}

但是,另一方面:可能重复到:虽然我会使用类似的方法,但我不会将所有内容都放在一个正则表达式中。当然,这样做要短得多,但是如果某个文件的标记顺序不同(这对于一般的xml解析来说并不重要),那么也很容易出错。
public static void main(String[] args) throws Exception
{
    String input = "<name>remove use case</name>\n    <ruleId>2161</ruleId>\n    <ruleVersion>0.0.1</ruleVersion>\n    <ruleStatus>New</ruleStatus>\n    <nuggetId>489505737</nuggetId>\n    <icVersionId>50449</icVersionId>\n    <rlVersion>1.0</rlVersion>\n    <modelVersion>1.0</modelVersion>\n    <attributes>\n        <attribute>\n            <attributeName/>\n            <value/>\n        </attribute>\n    </attributes>\n    <notes></notes>";
    String filename = input.replaceAll("(?s).*<name>(.*)</name>.*<ruleId>(.*)</ruleId>.*<ruleVersion>(.*)</ruleVersion>.*", "$1_$2_$3.xml");
    System.out.println(filename);
}
remove use case_2161_0.0.1.xml