合并两个web.xml文件,避免重复节点

时间:2016-03-08 07:51:09

标签: java xml

我正在尝试合并两个web.xml文件。我尝试了很多选项,但他们没有做一个很好的差异来识别真正缺少要合并的节点。

我正在考虑使用java自己编写它。策略是将两个web.xml中的一个保留为基本文件,然后在节点的基础上读取另一个,记住典型的web.xml结构。像顶级节点一样,将有一些context-param条目,然后是一些过滤器和过滤器映射,然后是服务器和servlet映射。现在要合并我需要在逻辑上检查重复项,如servlet条目到servlet entires等。

关于如何去做的任何建议

1 个答案:

答案 0 :(得分:1)

我的工作是从另一个文件自动构建一个xml。头部和身体在使用中很常见。 这是我的代码:

/**
 * auto bulid xmlFile
 * @param config        source "D:/config.txt"
 * @param outputxml    output path 
 * @param head      xml one
 * @param body      xml two
 * @throws Exception     
 */

public static void getXml(String config,String outputxml,String head,String body) throws Exception {
    FileInputStream fis = new FileInputStream(config);
    FileOutputStream fos=new FileOutputStream(outputxml);
    OutputStreamWriter writer=new OutputStreamWriter(fos);
    FileInputStream sin=new FileInputStream(head);
    FileInputStream sin2=new FileInputStream(body);
    InputStreamReader reader = new InputStreamReader(sin, "GBK");
    BufferedReader br = new BufferedReader(reader);
    String str1=null;
    while ((str1 = br.readLine()) != null) {
        writer.write(str1+"\n");
        writer.flush();
    }
    reader = new InputStreamReader(fis, "GBK");
    br = new BufferedReader(reader);
    String str = null;
    int id = 11,id2=6,f=0,f2=0;//start id
    while ((str = br.readLine()) != null) {
        String[] arr = str.split("\t");
        String xmlData=new String();
        if("I".equals(arr[4].trim())){
            xmlData += " nullable=\"true\"";
            id ++;
            f=0;
        }else{
            xmlData += " nullable=\"false\"";
            id2++;
            f=1;
        }
        if(f2!=f){
            InputStreamReader reader2 = new InputStreamReader(sin2, "GBK");
            BufferedReader br2 = new BufferedReader(reader2);
            String str3=null;
            while ((str3 = br2.readLine()) != null) {
                writer.write(str3+"\n");
                writer.flush();
            }
            br2.close();
            reader2.close();
        }
        xmlData += " logmask=\"\"";
        xmlData += "/>";
        f2=f;
        if(f==0){
            xmlData= "<Field id=\""+id+"\" name=\""+arr[1]+"\" desc=\""+arr[0]+"\" len=\""+arr[2].substring(arr[2].indexOf("(")+1, arr[2].indexOf(")"))+"\" filltype=\"0\" fillcontent=\"\" loop=\"\" loopcountcol=\"\" loopgap=\"\" defaultvalue=\"\" packtype=\"2\" tag=\""+arr[6]+"\" substitute=\"false\" bytevalues=\"\""+xmlData;
            writer.write("\t"+xmlData+"\n");
        }else{
            xmlData = "<Field id=\""+id2+"\" name=\""+arr[1]+"\" desc=\""+arr[0]+"\" len=\""+arr[2].substring(arr[2].indexOf("(")+1, arr[2].indexOf(")"))+"\" filltype=\"0\" fillcontent=\"\" loop=\"\" loopcountcol=\"\" loopgap=\"\" defaultvalue=\"\" packtype=\"2\" tag=\""+arr[6]+"\" substitute=\"false\" bytevalues=\"\""+xmlData;
            writer.write("\t"+xmlData+"\n");
        }
        System.out.println(xmlData);
    }
    writer.write("</ReturnPackInfo></Pack>");
    br.close();
    reader.close();
    fis.close();
    sin.close();
    sin2.close();
    writer.close();
}

config.txt的:

AA  AccType char(1) 1-  I   Y   service/AccType
AA  Account char(32)    33  I   Y   service/Account
AA  IdType  char(10)    33  I   N   service/IdType
AA  TACode  char(9)     I   N   service/TACode
AA  PrdCode char(20)        I   N   service/PrdCode
AA  OffSet  char(8) -1  I   Y   service/OffSet
AA  QueryNum    char(8) -10 I   Y   service/QueryNum
AA  ClientType  char(1) 03  I   N   service/ClientType
AA  HostClientNo    char(20)        I   N   service/HostClientNo
AA  PrdType char(1) 02  I       service/PrdType
AA  TotNum  char(8)     O       service/TotNum
AA  RetNum  char(8)     O       service/RetNum
AA  OffSet  char(8)     O       service/OffSet
AA  DOWN_FILE_NAME     char(50)     O       service/DOWN_FILE_NAME
AA  DOWN_COUNT     char(20)     O       service/DOWN_COUNT

头文件

<?xml version="1.0" encoding="UTF-8"?>

<Pack>
  <Content>
    <MsgType></MsgType>
    <PackCode></PackCode>
    <Desc></Desc>
    <Target>termqxgt</Target>
    <Encoding>UTF-8</Encoding>
    <Trace>true</Trace>
  </Content>
  <LengthInfo>
    <Enable>true</Enable>
    <HeadFlag>true</HeadFlag>
    <HexFlag>false</HexFlag>
    <AsciiFlag>false</AsciiFlag>
    <HighFlag>false</HighFlag>
    <InfoLen>8</InfoLen>
    <SelfFlag>false</SelfFlag>
    <FillType>2</FillType>
    <Filling>0</Filling>
  </LengthInfo>
  <SendPackInfo septype="1" separator="" leadingflag="false" leadingtype="" leadingcontent="">
    <Field id="1" name="XMLHead" desc="xx" len="39" filltype="0" fillcontent="" loop="" loopcountcol="" loopgap="" defaultvalue="" packtype="1" tag="" substitute="false" bytevalues="" nullable="true" logmask=""/>

正文:

</SendPackInfo>
  <ReturnPackInfo septype="1" separator="" leadingflag="false" leadingtype="" leadingcontent="">
    <Field id="1" name="XMLHead" desc="XM" len="39" filltype="0" fillcontent="" loop="" loopcountcol="" loopgap="" defaultvalue="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;" packtype="1" tag="" substitute="false" bytevalues="" nullable="false" logmask=""/>