简体   繁体   中英

How to output the difference between two xml files into another xml file

I need to compare two XML files and print the results in another xml files This code compares and print the differences in the console correctly,but it prints only one line in the XML file.Moreover when I view the output in the console,it seems it is creating n number of xml files equal to the totaldifferences. Any help is appreciated.Thanks in advance.

public class CreateXMLFileJava {
public static final String xmlFilePath = "C:\\test\\xmlfile.xml";
public static void main(String argv[]) throws SAXException, IOException, ParserConfigurationException, TransformerException
{

           FileInputStream fis1 = new FileInputStream("c:\\test\\source.xml");
           FileInputStream fis2 = new FileInputStream("c:\\test\\target.xml");

           BufferedReader  source = new BufferedReader(new InputStreamReader(fis1));
           BufferedReader  target = new BufferedReader(new InputStreamReader(fis2));

           XMLUnit.setIgnoreWhitespace(true);

           List<String> differences = compareXML(source, target);
           try {
               printDifferences(differences);
           } catch (ParserConfigurationException e) {
               e.printStackTrace();
           } catch (TransformerException e) {
               e.printStackTrace();
           }
}
public static void buildxml(String st, int totalDifferences)
{
    DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();

    DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();

    Document document = documentBuilder.newDocument();

    Element root = document.createElement("Errorlist");
    document.appendChild(root);

    Element number = document.createElement("error");

    root.appendChild(number);

    Attr attr = document.createAttribute("i");
    attr.setValue(st);
    number.setAttributeNode(attr);

   // create the xml file
   //transform the DOM Object to an XML File

   TransformerFactory transformerFactory = TransformerFactory.newInstance();
   Transformer transformer = transformerFactory.newTransformer();
   DOMSource domSource = new DOMSource(document);
   StreamResult streamResult = new StreamResult(new File(xmlFilePath));
   transformer.transform(domSource, streamResult);
   System.out.println("Done creating XML File");

}

public static List<String> compareXML(Reader source, Reader target) throws
SAXException, IOException
{

   Diff xmlDiff = new Diff(source, target);

   DetailedDiff detailXmlDiff = new DetailedDiff(xmlDiff);

   return detailXmlDiff.getAllDifferences();
}

public static void printDifferences(List<String> differences) throws IOException, ParserConfigurationException, TransformerException
{
   int totalDifferences = differences.size();
   System.out.println("===============================");
   System.out.println("Total differences : " + totalDifferences);
   System.out.println("================================");
   for (Iterator<String> iterator = differences.iterator(); iterator.hasNext();) {
       Object s=(Object)iterator.next();
       String st=s.toString();
       System.out.println(st);
       buildxml(st,totalDifferences);
}

According your code, method buildxml create new file each iteration over your differences List. But your need create DOMmodel, in each iteration add new element( appendChild ) and only then create File.

You can fix it like that. Delete method printDifferences and transfer logic to buildXml

public class CreateXMLFileJava {
public static final String xmlFilePath = "C:\\test\\xmlfile.xml";
public static void main(String argv[]) throws SAXException, IOException, ParserConfigurationException, TransformerException
{

           FileInputStream fis1 = new FileInputStream("c:\\test\\source.xml");
           FileInputStream fis2 = new FileInputStream("c:\\test\\target.xml");

           BufferedReader  source = new BufferedReader(new InputStreamReader(fis1));
           BufferedReader  target = new BufferedReader(new InputStreamReader(fis2));

           XMLUnit.setIgnoreWhitespace(true);

           List<String> differences = compareXML(source, target);
           try {
               printDifferences(differences);
           } catch (ParserConfigurationException e) {
               e.printStackTrace();
           } catch (TransformerException e) {
               e.printStackTrace();
           }
}
public static void buildxml(String st, List<String> differences)
{
    DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();

    DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();

    Document document = documentBuilder.newDocument();

    Element root = document.createElement("Errorlist");
    document.appendChild(root);

    int totalDifferences = differences.size();
    System.out.println("===============================");
    System.out.println("Total differences : " + totalDifferences);
    System.out.println("================================");

    for (Iterator<String> iterator = differences.iterator(); iterator.hasNext();) {
       Object s=(Object)iterator.next();
       String st=s.toString();
       System.out.println(st);

       Element number = document.createElement("error");
       Attr attr = document.createAttribute("i");
       attr.setValue(st);
       number.setAttributeNode(attr);

       root.appendChild(number);
   }

   // create the xml file
   //transform the DOM Object to an XML File

   TransformerFactory transformerFactory = TransformerFactory.newInstance();
   Transformer transformer = transformerFactory.newTransformer();
   DOMSource domSource = new DOMSource(document);
   StreamResult streamResult = new StreamResult(new File(xmlFilePath));
   transformer.transform(domSource, streamResult);
   System.out.println("Done creating XML File");

}

public static List<String> compareXML(Reader source, Reader target) throws
SAXException, IOException
{

   Diff xmlDiff = new Diff(source, target);

   DetailedDiff detailXmlDiff = new DetailedDiff(xmlDiff);

   return detailXmlDiff.getAllDifferences();
}

I have made few changes in my code to display the output message along with xml

public static void printDifferences(List<String> differences) throws IOException, ParserConfigurationException, TransformerException
{

    int totalDifferences = differences.size();
    System.out.println("===============================");
    System.out.println("Total differences : " + totalDifferences);
    System.out.println("================================");

    int count = 0;
    List<Object> l2=new ArrayList<>();
    for (Iterator<String> iterator = differences.iterator(); iterator.hasNext();) {
        Object s=(Object)iterator.next();
        l2.add(s);
        count++;
        buildxml(l2,count);
    }
    System.out.println("Done creating XML File");
}
    public static void buildxml(List l, int count)
            throws ParserConfigurationException, TransformerException {
        int i=0;
        DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
        Document document = documentBuilder.newDocument();
        Element root = document.createElement("Errorlist");
        document.appendChild(root);
        for(Object ob:l) {
            Element error = document.createElement("error");
            root.appendChild(error);
            error.setAttribute("id", String.valueOf(++i));
            Element number=document.createElement("ERR");
            number.appendChild(document.createTextNode(ob.toString()));
            error.appendChild(number);
        }

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource domSource = new DOMSource(document);
        StreamResult streamResult = new StreamResult(new File(xmlFilePath));
        //StreamResult streamResult = new StreamResult(System.out);

        transformer.transform(domSource, streamResult);

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM