2015-12-22 203 views
-3

我有兩個xml文件如下。如何比較並獲得使用java的兩個XML文件之間的差異?

Compare.xml

<?xml version="1.0"?> 
<class> 
<student rollno="393"> 
    <firstname>dinkar</firstname> 
    <lastname>kad</lastname> 
    <nickname>dinkar</nickname> 
    <marks>85</marks> 
</student> 
<student rollno="493"> 
    <firstname>Vaneet</firstname> 
    <lastname>Gupta</lastname> 
    <nickname>vinni</nickname> 
    <marks>95</marks> 
</student> 
</class> 

Reference.xml

<?xml version="1.0"?> 
<class> 
<student rollno="393"> 
    <firstname>ila</firstname> 
    <lastname>kad</lastname> 
    <nickname>dinkar</nickname> 
    <marks>85</marks> 
</student> 
<student rollno="493"> 
    <firstname>Vaneet</firstname> 
    <lastname>Gupta</lastname> 
    <nickname>vinni</nickname> 
    <marks>95</marks> 
</student> 
</class> 

現在我需要比較並獲得這兩個XML文件之間的差異。我還需要將輸出作爲日誌文件導出。

下面我的代碼:

package DomParserDemo; 
import java.io.File; 
import java.util.logging.FileHandler; 
import java.util.logging.Logger; 

import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.DocumentBuilder; 

import org.w3c.dom.Document; 
import org.w3c.dom.NodeList; 
import org.w3c.dom.Node; 
import org.w3c.dom.Element; 

public class DomParserDemo { 
public static void main(String[] args){ 

    try { 
    File inputFile = new File("input.txt"); 
    DocumentBuilderFactory dbFactory 
     = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
    Document doc = dBuilder.parse(inputFile); 
    doc.getDocumentElement().normalize(); 
    System.out.println("Root element :" 
     + doc.getDocumentElement().getNodeName()); 
    NodeList nList = doc.getElementsByTagName("student"); 
    System.out.println("----------------------------"); 
    for (int temp = 0; temp < nList.getLength(); temp++) { 
     Node nNode = nList.item(temp); 
     System.out.println("\nCurrent Element :" 
      + nNode.getNodeName()); 
     if (nNode.getNodeType() == Node.ELEMENT_NODE) { 
      Element eElement = (Element) nNode; 
      System.out.println("Student roll no : " 
       + eElement.getAttribute("rollno")); 
      System.out.println("First Name : " 
       + eElement 
       .getElementsByTagName("firstname") 
       .item(0) 
       .getTextContent()); 
      System.out.println("Last Name : " 
      + eElement 
       .getElementsByTagName("lastname") 
       .item(0) 
       .getTextContent()); 
      System.out.println("Nick Name : " 
      + eElement 
       .getElementsByTagName("nickname") 
       .item(0) 
       .getTextContent()); 
      System.out.println("Marks : " 
      + eElement 
       .getElementsByTagName("marks") 
       .item(0) 
       .getTextContent()); 
     } 
    } 

    FileHandler handler = new FileHandler("logfile.log"); 

    // Add to the desired logger 
    Logger logger = Logger.getLogger(""); 
    logger.addHandler(handler); 
    System.out.println("Log Generated Successfully...!!!"); 


    } catch (Exception e) { 
     System.out.println("Log Generation Failed..!!!"); 
    e.printStackTrace(); 
    } 
} 

} 

現在我可以看到在輸出的XML文件以進行標記,我需要它的差異和輸出應該導出爲一個日誌文件。

請幫助我完成此事,並提前致謝。

+1

你可以使用'xpath'生成基於節點上的一個文檔中的查詢和查詢等,如果查詢不返回匹配結果,你知道你的第一個節點在你的第一個節點中沒有出現。您可以爲這兩個文件 – MadProgrammer

+0

可能的副本做到這一點:http://stackoverflow.com/questions/31439859/algorithm-for-identifying-differences-in-xml-documents/34203098#34203098 –

回答

0

謝謝。 我現在的代碼: package compare5;

import org.apache.log4j.Logger; 

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.List; 

import org.custommonkey.xmlunit.DetailedDiff; 
import org.custommonkey.xmlunit.Diff; 
import org.custommonkey.xmlunit.Difference; 
import org.xml.sax.SAXException; 

public class ComparisonTest { 

final static Logger logger = Logger.getLogger(ComparisonTest.class); 

public static void main(String[] args) { 
File f1 = new File("D:/reference.xml"); 
File f2= new File("D:/comparison.xml"); 
File f3= new File("D:/Activity log.log"); 

try { 
          //create a new file if it doesn't 
      f3.createNewFile(); 

    } catch (IOException e) { 

        e.printStackTrace(); 
          } 
FileReader fr1 = null; 
FileReader fr2 = null; 
try { 
fr1 = new FileReader(f1); 
fr2 = new FileReader(f2); 
} catch (FileNotFoundException e) { 
e.printStackTrace(); 
            } 

try { 
Diff diff = new Diff(fr1, fr2); 
System.out.println("Similar? " + diff.similar()); 
System.out.println("Identical? " + diff.identical()); 

DetailedDiff detDiff = new DetailedDiff(diff); 
List differences = detDiff.getAllDifferences(); 
for (Object object : differences) { 
    Difference difference = (Difference)object; 
    System.out.println("***********************"); 
    System.out.println(difference); 
    System.out.println("***********************"); 
    } 

} catch (SAXException e) { 
e.printStackTrace(); 
} catch (IOException e) { 
e.printStackTrace(); 
} 


ComparisonTest obj = new ComparisonTest(); 
obj.runMe("ila"); 


} 

private void runMe(String parameter){ 



if(logger.isDebugEnabled()){ 
    logger.debug("This is debug : " + parameter); 
} 

if(logger.isInfoEnabled()){ 
    logger.info("This is info : " + parameter); 
} 

logger.warn("This is warn : " + parameter); 
logger.error("This is error : " + parameter); 
logger.fatal("This is fatal : " + parameter); 



} 






} 
0

試試這個

private void compare(File file1, File file2) throws Exception { 
    List<String> list1 = Files.lines(Paths.get(file1.getPath())).collect(Collectors.toList()); 
    List<String> list2 = Files.lines(Paths.get(file2.getPath())).collect(Collectors.toList()); 
    list1.stream().filter(s -> !list2.contains(s)).peek(System.out::println).count() != 0; 
    list2.stream().filter(s -> !list1.contains(s)).peek(System.out::println).count() != 0; 
} 
相關問題