从一个XML读取值到另一个XML

时间:2022-10-09 07:37:10

I have two XMLs (Queries.xml and EnvVar.xml) the first xml contains all my SQL queries my second xml contains the paramerters/environment variables etc.,

我有两个XML(Queries.xml和EnvVar.xml),第一个xml包含我的所有SQL查询,第二个xml包含参数/环境变量等,

In Queries.xml each query is inside a tag, now for the WHERE clause in queries I want to read the conditions from EnvVar.xml, for example Select * from table WHERE Year in (2014,2015)... (2014,2015) comes from EnvVar.xml

在Queries.xml中,每个查询都在一个标记内,现在对于查询中的WHERE子句我想从EnvVar.xml读取条件,例如Select * from table WHERE Year in(2014,2015)...(2014,2015 )来自EnvVar.xml

Queries.xml as below:

Queries.xml如下:

<Queries> <Sql1> Select * from table Where Year in() </Sql1> </Queries>

EnvVar.xml as below:

EnvVar.xml如下:

<Parameters><Year>2014,2015</Year></Parameters>

With the below Java code I am able to read whats inside Sql1 is there a way to send year value from EnvVar to Queries?

使用下面的Java代码,我能够读取Sql1中的内容是否有办法将年份值从EnvVar发送到查询?

public static String ReportQuery(String TagValue){
    String HTMLGlobalParameters=null;
    try {           
        URL url = Test.class.getClassLoader().getResource("Queries.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(url.getPath());
        doc.getDocumentElement().normalize();
        NodeList nList = doc.getElementsByTagName("Queries");
        for (int temp = 0; temp < nList.getLength(); temp++) {
            Node nNode = nList.item(temp);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;
                HTMLGlobalParameters = getTagValue(TagValue, eElement);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return HTMLGlobalParameters;

}

1 个解决方案

#1


0  

I suggest you to change Queries.xml to:

我建议你将Queries.xml更改为:

<Queries> <Sql1> Select * from table Where Year in(@Year@) </Sql1> </Queries>

Then when you have in EnvVar.xml:

然后当您在EnvVar.xml中时:

<Parameters><Year>2014,2015</Year></Parameters>

Your tag is Year so try to find '@'+Tag.Name+'@' or '@Year@' in Queries.xml tag-value -or your query string- and replace it to EnvVar.xml tag-value -here if 2014,2015-.

你的标签是年份,所以试着在Queries.xml标签值中找到'@'+ Tag.Name +'@'或'@ Year @' - 或者你的查询字符串 - 并将其替换为EnvVar.xml tag-value -here if 2014,2015-。


Your ReportQuery method with a little change can be a new method for getting a tag-value of EnvVar.xml file with a name like ReportWhereClues.

您稍微更改的ReportQuery方法可以是获取EnvVar.xml文件的标记值的新方法,其名称类似于ReportWhereClues。

Now before return HTMLGlobalParameters; line in ReportQuery you can check HTMLGlobalParameters that there is any part of it like @...@, and by using ReportWhereClues with an argument that is the string between those @ you can get the tag-value like 2014,2015, Now replace @...@ to that tag-value.

现在返回HTMLGlobalParameters之前;在ReportQuery中你可以查看HTMLGlobalParameters,它有@@ @的任何部分,并且使用ReportWhereClues,其参数是@之间的字符串,你可以得到像2014,2015这样的标记值,现在替换@ ...... @到那个标签值。

#1


0  

I suggest you to change Queries.xml to:

我建议你将Queries.xml更改为:

<Queries> <Sql1> Select * from table Where Year in(@Year@) </Sql1> </Queries>

Then when you have in EnvVar.xml:

然后当您在EnvVar.xml中时:

<Parameters><Year>2014,2015</Year></Parameters>

Your tag is Year so try to find '@'+Tag.Name+'@' or '@Year@' in Queries.xml tag-value -or your query string- and replace it to EnvVar.xml tag-value -here if 2014,2015-.

你的标签是年份,所以试着在Queries.xml标签值中找到'@'+ Tag.Name +'@'或'@ Year @' - 或者你的查询字符串 - 并将其替换为EnvVar.xml tag-value -here if 2014,2015-。


Your ReportQuery method with a little change can be a new method for getting a tag-value of EnvVar.xml file with a name like ReportWhereClues.

您稍微更改的ReportQuery方法可以是获取EnvVar.xml文件的标记值的新方法,其名称类似于ReportWhereClues。

Now before return HTMLGlobalParameters; line in ReportQuery you can check HTMLGlobalParameters that there is any part of it like @...@, and by using ReportWhereClues with an argument that is the string between those @ you can get the tag-value like 2014,2015, Now replace @...@ to that tag-value.

现在返回HTMLGlobalParameters之前;在ReportQuery中你可以查看HTMLGlobalParameters,它有@@ @的任何部分,并且使用ReportWhereClues,其参数是@之间的字符串,你可以得到像2014,2015这样的标记值,现在替换@ ...... @到那个标签值。