Vik
Vik

Reputation: 9319

merging two web.xml files avoiding duplicate nodes

I am trying to merge two web.xml files. There are many options i tried but they fail to do a nice diff to identify the truly missing nodes to merge.

I am thinking to write it now myself using java. strategy would be to keep one of the two web.xml as base file and then read another one on node basis keeping typical web.xml structure in mind. like top level node will have some context-param entries, then some filter and filter mapping and then server and servlet mappings. now to merge i need to check duplicates logically like servlet entries into servlet entires etc.

any advise on how to go about it

Upvotes: 0

Views: 285

Answers (1)

Dlucy
Dlucy

Reputation: 19

My work is auto build a xml from another file.The head and body is common in use. here is my code:

/**
 * 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

head file

<?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=""/>

body file:

</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=""/>

Upvotes: 1

Related Questions