从XML文件中的占位符获取变量名的Java程序

从XML文件中的占位符获取变量名的Java程序,java,xml,jersey,Java,Xml,Jersey,希望你们都好。 我有一个要求,需要从XML文件中可用的占位符中选择变量名。 我有一个XML文件,其中包含所有占位符,这些占位符以$symbol开头。 我的任务是获取占位符,并从中获取变量名 例如,如果XML文件具有类似$Variable1的占位符,那么它将从该占位符中获取Variable1 以下是我正在使用的代码: public static String replaceConfigParam(String xmlFile, Object structure) { for (String

希望你们都好。 我有一个要求,需要从XML文件中可用的占位符中选择变量名。 我有一个XML文件,其中包含所有占位符,这些占位符以$symbol开头。 我的任务是获取占位符,并从中获取变量名

例如,如果XML文件具有类似$Variable1的占位符,那么它将从该占位符中获取Variable1

以下是我正在使用的代码:

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;
下面是getConstant方法:

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;
}
私有静态列表getConstants(字符串域配置){
字符串[]arr=domainConfig.split(\\$);
List ParamsTracted=新建ArrayList();
for(字符串键:arr){
paramstracted.add(key.replace(“$”,”);
}
返回参数跟踪;
}
以下是XML文件,其中包含$,我需要从同一文件中提取变量:

<tunnel>
        <units>
          <entry name="tunnel.1">
            <ip>
              <entry name="$ABC"/>
            </ip>
            <interface-management-profile>mgt</interface-management-profile>
          </entry>
        </units>
      </tunnel>

管理

提前谢谢。

仍然不确定你到底在问什么。如果你有一个特定的问题,你应该描述这个问题。明确你想知道什么,你的问题在哪里,你需要什么帮助

我假设你的问题是:

“如何提取开头由$符号标识的所有变量的entry name属性”。

您可以使用正则表达式实现这一点,但由于您使用的是XML,因此可以使用Xpath+Xpath解析器。请看这里:

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class ExtractVar {

    static String xml = "<tunnel>" +
    "<units>" +
      "<entry name=\"tunnel.1\">"+
        "<ip>"+
       "   <entry name=\"$ABC\"/>"+
      "  </ip>"+
     "   <interface-management-profile>mgt</interface-management-profile>"+
    "  </entry>"+
   " </units>"+
  "</tunnel>";


    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document document = db.parse(new InputSource(new StringReader(xml)));

        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPathExpression xpath = xpathFactory.newXPath().compile("//entry");

        NodeList entryNodes = (NodeList) xpath.evaluate(document, XPathConstants.NODESET);
        for(int i =0; i<entryNodes.getLength(); i++) {
            Node n = entryNodes.item(i);
            String nodeValue = n.getAttributes().getNamedItem("name").getNodeValue();
            if(nodeValue.startsWith("$")) {
                System.out.println(nodeValue.substring(1, nodeValue.length()));
            }
        }
    }
}
我希望这就是你想要的

或者,这可以通过纯正则表达式捕获每个由引号字符包围并以美元符号开头的字符串来实现,如下所示:

    Pattern pattern = Pattern.compile("\"\\$(.*)\"");
    Matcher matcher = pattern.matcher(xml);
    while(matcher.find()) {
        System.out.println(matcher.group(1));
    }

阿图尔仍然不确定你到底在问什么。如果你有一个特定的问题,你应该描述这个问题。明确你想知道什么,你的问题在哪里,你需要什么帮助

我假设你的问题是:

“如何提取开头由$符号标识的所有变量的entry name属性”。

您可以使用正则表达式实现这一点,但由于您使用的是XML,因此可以使用Xpath+Xpath解析器。请看这里:

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class ExtractVar {

    static String xml = "<tunnel>" +
    "<units>" +
      "<entry name=\"tunnel.1\">"+
        "<ip>"+
       "   <entry name=\"$ABC\"/>"+
      "  </ip>"+
     "   <interface-management-profile>mgt</interface-management-profile>"+
    "  </entry>"+
   " </units>"+
  "</tunnel>";


    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document document = db.parse(new InputSource(new StringReader(xml)));

        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPathExpression xpath = xpathFactory.newXPath().compile("//entry");

        NodeList entryNodes = (NodeList) xpath.evaluate(document, XPathConstants.NODESET);
        for(int i =0; i<entryNodes.getLength(); i++) {
            Node n = entryNodes.item(i);
            String nodeValue = n.getAttributes().getNamedItem("name").getNodeValue();
            if(nodeValue.startsWith("$")) {
                System.out.println(nodeValue.substring(1, nodeValue.length()));
            }
        }
    }
}
我希望这就是你想要的

或者,这可以通过纯正则表达式捕获每个由引号字符包围并以美元符号开头的字符串来实现,如下所示:

    Pattern pattern = Pattern.compile("\"\\$(.*)\"");
    Matcher matcher = pattern.matcher(xml);
    while(matcher.find()) {
        System.out.println(matcher.group(1));
    }

Artur

这里没有问题?我已经问了上面的问题?我的意思是,没有以“我该怎么做…”“我该怎么做…”开头的句子似乎你已经回答了你自己的问题?什么不起作用?你有例外吗?您是否存在性能问题?XML看起来像什么?我真的不知道你到底需要什么帮助:)@pandaadb,我已经添加了XML文件,它有占位符,以$symbol(即$ABC)开头。这里没有问题吗?上面我问了这个问题?我的意思是,实际上没有以“我怎么做…”“我做什么…”开头的句子看来你已经回答了你自己的问题了?什么不起作用?你有例外吗?您是否存在性能问题?XML看起来像什么?我真的不知道你到底需要什么帮助:)@pandaadb,我已经添加了带有占位符的XML文件,从$symbol(即$ABC)开始。我们如何在下面添加<和>:Pattern Pattern=Pattern.compile(“\”\$(.*)\”);您想要捕获的到底是什么?我们如何在以下内容中添加<和>:Pattern=Pattern.compile(“\”\$(.*)\”);你到底想捕捉什么?