希望你們都好。 我有一個需求,我需要從XML文件中可用的佔位符中選擇變量名稱。 我有一個XML文件具有所有佔位符,並且這些佔位符以$符號開頭。 我的任務是獲得該佔位符,並從它我需要獲取變量名稱。Java程序從XML文件中提供的佔位符中獲取變量名
例如,如果XML文件具有像$ Variable1那樣的佔位符,那麼它將從該佔位符獲得Variable1。
以下是我使用的代碼:以下是getConstant方法
public static String replaceConfigParam(String xmlFile, Object structure) {
for (String key : getConstants(xmlFile)) {
String actualKey = (new StringBuilder().append("$*").append(key).append("$")).toString();
try {
String value = BeanUtils.getProperty(structure, key);
if (value != null) {
xmlFile = xmlFile.replace(actualKey, value);
}
} catch (IllegalAccessException e) {
logger.error("failed to get the property from object " + e.getMessage());
} catch (InvocationTargetException e) {
logger.error("failed to get the property from object" + e.getMessage());
} catch (NoSuchMethodException e) {
logger.error("failed to get the property from object " + e.getMessage());
} catch (Exception e) {
logger.error("failed to get value from the property " + e.getMessage());
}
}
return xmlFile;
:
private static List<String> getConstants(String domainConfig) {
String[] arr = domainConfig.split("\\$");
List<String> paramsExtracted = new ArrayList<String>();
for (String key : arr) {
paramsExtracted.add(key.replace("$", ""));
}
return paramsExtracted;
}
以下是有它的美元XML文件,我需要提取變量從相同的文件:
<tunnel>
<units>
<entry name="tunnel.1">
<ip>
<entry name="$ABC"/>
</ip>
<interface-management-profile>mgt</interface-management-profile>
</entry>
</units>
</tunnel>
在此先感謝。
沒有問題嗎? – pandaadb
以上我問過這個問題? – Prax
我的意思是說,沒有句子以「我該怎麼做」開頭。「我該怎麼做......」這看起來像你已經回答了你自己的問題?什麼不行?你有例外嗎?你有性能問題嗎? XML的外觀是什麼?我從字面上不知道它是什麼,你需要幫助:) http://stackoverflow.com/help/how-to-ask – pandaadb