0

我需要比较两个 XML 文件并在另一个 xml 文件中打印结果此代码比较并正确打印控制台中的差异,但它仅在 XML 文件中打印一行。此外,当我在控制台中查看输出时,似乎它正在创建 n 个等于总差异的 xml 文件。任何帮助表示赞赏。在此先感谢。

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);
}
4

2 回答 2

0

我对代码进行了一些更改以显示输出消息和 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);

    }

}
于 2020-02-12T06:46:03.840 回答
0

根据您的代码,方法buildxml在您的差异列表的每次迭代中创建新文件。但是您需要创建 DOMmodel,在每次迭代中添加新元素(appendChild),然后才创建文件。

你可以这样修复它。删除方法printDifferences并将逻辑转移到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();
}
于 2020-02-11T07:39:26.150 回答