gpt4 book ai didi

Java - 使用模板从字符串中提取键值对

转载 作者:行者123 更新时间:2023-12-04 12:59:53 24 4
gpt4 key购买 nike

所以在 org.apache.commons.lang3.text 中有一个名为 StrSubstitutor 的类,它可以接受这样的映射:

Map<String, String> params = new HashMap<String, String>();
params.put("name","Vivek");
params.put("id","900");
params.put("somethingelse","blah");

和这样的模板字符串:

    <data>
<id>${id}</id>
<name>${name}</name>
<something>${somethingelse}</something>
</data>

生成这样的输出字符串:

   <data>
<id>900</id>
<name>Vivek</name>
<something>blah</something>
</data>

我想要的恰恰相反。有没有一种方法可以将输出字符串和模板以模板变量作为键并以字符串中的相应值作为值来填充 map ?

PS - 我将使用的字符串不一定总是 XML。这只是一个例子。

编辑:我认为有些人感到困惑,因为变量名称和标签名称相同。标签只是说明性的,与问题无关。我关心的是 ${} 中的变量。我添加了另一个标签来说明我的意思。

最佳答案

这是另一种选择:

import java.util.HashMap;
import java.util.Map;

public class Test{

public static void main(String[] args){

//simulate template. Assuming no more than on param in line
String[] template = new String[]{
"<data>",
"<id>${id}</id>",
"<name>${name}</name>",
"<something>${somethingelse}</something>",
"</data>"
};

String[] output = new String[]{
"<data>",
"<id>900</id>",
"<name>Vivek</name>",
"<somethingelse>blah</somethingelse>",
"</data>"
};

Map<String, String> params = getParams(template);

getValues(params, output);

for(String key : params.keySet()) {
System.out.println(key +" : " + params.get(key));
}
}

private static Map<String, String> getParams(String[] template) {

Map<String, String> params = new HashMap<String, String>();

for (String line : template) {

//get location of 3 chars ${}
int index$ = line.indexOf("$");
int indexLeftB = line.indexOf("{");
int indexRightB = line.indexOf("}");

//make sure all ${} are present
if((index$ <0) || (indexLeftB <0) || (indexRightB <0) ) {
continue;
}

//make sure they are in the right order
if( ((indexLeftB - index$) !=1) || (indexRightB < indexLeftB)) {
continue;
}

//get param
String param = getParamFromLine(line, indexLeftB+1 , indexRightB);

if(param != null) {

params.put(param,null);
}
}

return params;
}

private static void getValues(Map<String, String> params, String[] output) {

//iterate over map
for(String param : params.keySet()) {

String tag = "<"+param+">"; //like <name>
String closeTag = "</"+param+">"; //like <name>

//iterate over output
for(String line : output) {

line = line.trim(); //remove all whitespace
//look for first occurrence of patternToSearch
int index1 = line.indexOf(tag, 0);
int index2 = line.indexOf(closeTag, index1);

//make sure there are 2 occurrences in
if((index1 < 0) || (index2 < 0)) {
continue;
}

String value = getParamFromLine(line, index1+ tag.length(), index2);
if(value != null) {

params.put(param, value);
}
}
}
}

private static String getParamFromLine(String line, int indexLeftB, int indexRightB) {

String param = line.substring(indexLeftB, indexRightB);

return (param.trim().length() == 0) ? null : param.trim();
}
}

关于Java - 使用模板从字符串中提取键值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39813198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com