• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Program to find diff between two xml files

 
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

i need to write a Java Program that will create an xml file based on the difference between two xml file.
For eg: say i have two xml files 1.xml and 2.xml .

1.xml
-----

<?xml version= '1.0'?>
<!DOCTYPE ADDRESSBOOK [
<!ELEMENT ADDRESSBOOK (PERSON)*>
<!ELEMENT PERSON (FIRSTNAME, COMPANY)>
<!ELEMENT FIRSTNAME (#PCDATA)>
<!ELEMENT COMPANY (#PCDATA)>
]>
<ADDRESSBOOK>

<PERSON>
<FIRSTNAME>Doe</FIRSTNAME>
<COMPANY>Doe Enterprise, Inc.</COMPANY>
</PERSON>

<PERSON>
<FIRSTNAME>Doe</FIRSTNAME>
<COMPANY>Doe Enterprise, Inc.</COMPANY>
</PERSON>

</ADDRESSBOOK>

2.xml
-------

<?xml version= '1.0'?>
<!DOCTYPE ADDRESSBOOK [
<!ELEMENT ADDRESSBOOK (PERSON)*>
<!ELEMENT PERSON (FIRSTNAME,EMAIL)>
<!ELEMENT FIRSTNAME (#PCDATA)>
<!ELEMENT EMAIL (#PCDATA)>
]>
<ADDRESSBOOK>

<PERSON>
<FIRSTNAME>Doe</FIRSTNAME>
<EMAIL>john@doeenterprise.com</EMAIL>
</PERSON>

<PERSON>
<FIRSTNAME>Doe</FIRSTNAME>
<EMAIL>john@doeenterprise.com</EMAIL>
</PERSON>

</ADDRESSBOOK>

Now i need to generate a third file 3.xml with the output as follows(In this file the elements common to 1.xml and 2.xml are deleted.Also they should be deleted only if there are no child elements for them.So here the PERSON elememt should not be deleted)

3.xml
-----
<?xml version= '1.0'?>
<!DOCTYPE ADDRESSBOOK [
<!ELEMENT ADDRESSBOOK (PERSON)*>
<!ELEMENT PERSON (COMPANY, EMAIL)>
<!ELEMENT COMPANY (#PCDATA)>
<!ELEMENT EMAIL (#PCDATA)>
]>
<ADDRESSBOOK>

<PERSON>
<COMPANY>Doe Enterprise, Inc.</COMPANY>
<EMAIL>john@doeenterprise.com</EMAIL>
</PERSON>

<PERSON>
<COMPANY>Doe Enterprise, Inc.</COMPANY>
<EMAIL>john@doeenterprise.com</EMAIL>
</PERSON>

</ADDRESSBOOK>

I should provide these files (ie,2 input files and one output file) as command line arguments .

Please revert as soon as possible

Thanks and Regards
Vidya
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Vidya. You must have missed our naming policy on the way in, but we ask that everyone use a real-sounding name, both a first and last name separated by a space. It keeps things friendly around here.
As for your question, while we love to help people, we ask that you at least Show Some Effort. We aren't going to Do Your Homework, but we will help you solve problems along the way.
Have you written any code? Do you have any specific questions or problems we can help with?
 
Ranch Hand
Posts: 88
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can try the tools/libraries available below:

http://www.java-tips.org/java-libraries/xml-diff-libraries/
 
Greenhorn
Posts: 1
jQuery Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The below blog link helps to get Diff b/w 2 xml in list of xpath format

http://jamesfarrugia.blogspot.com/2011/12/comparing-two-xml-documents-in-java.html

Using the above xpath list we can build xml output with the help of below code.




ArrayList<String> uniques are list of xpaths from the blog code.

Hope the above information helps...
Thanks.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
package gov.census.censuscentral.base;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.aventstack.extentreports.ExtentTest;

import gov.census.censuscentral.util.CensusCentralConstants;
import gov.census.censuscentral.util.FlagManager;

public class BasePage {

public static WebDriver driver;
public ExtentTest test;

/*@FindBy(xpath = SaferConstants.PAGE_TITLE)
public WebElement pagetitle;
@FindBy(xpath = SaferConstants.ZONESTAB_NEXTBUTTON)
public WebElement nextbutton;
@FindBy(xpath = SaferConstants.SPACES_ACTIVEELEMENTS)
public WebElement activeelements;
@FindBy(xpath = SaferConstants.RETURN_TO_TOP_LINK)
public WebElement returnToTopLnk;*/

long startTime = System.nanoTime();
long endTime = System.nanoTime();
boolean results;

// constructor

public BasePage(WebDriver driver, ExtentTest test) {

BasePage.driver = driver;
this.test = test;
PageFactory.initElements(driver, this);

}

public String demoFlag = FlagManager.getInstance().getConfigData("DEMO_FLAG");

// verify if element shows on webpage
public boolean isElementPresent(WebElement locator, ExtentTest node) throws IOException {
try {
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOf(locator));
wait.until(ExpectedConditions.elementToBeClickable(locator));

if (!locator.isDisplayed()) {
if (demoFlag.equalsIgnoreCase("Yes"))
node.fail("Element not found" + locator);
return false;
} else {
if (demoFlag.equalsIgnoreCase("Yes"))
node.pass("Element is present on the Page" + locator);
return true;
}

} catch (Exception e) {
node.fail("Inside isElementPresent method, Exception found: " + e.getMessage());
//node.addScreenCaptureFromPath(takeScreenShot("Inside isElementPresent method"));
takeScreenShot("Inside isElementPresent method", node);
return false;
}
}

public static String takeScreenShot(String screenshotName) throws IOException {
Date d = new Date();
String screenshotFile = d.toString().replace(":", "_").replace(" ", "_") + ".png";
String filePath = CensusCentralConstants.REPORTS_PATH + "screenshots//" + screenshotFile;
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
File finalDestination = new File(filePath);
FileUtils.copyFile(scrFile, finalDestination);
return filePath;
}

public void takeScreenShot(String screenshotName, ExtentTest node) throws IOException {
Date d = new Date();
String screenshotFile = d.toString().replace(":", "_").replace(" ", "_") + ".png";
String filePath = CensusCentralConstants.REPORTS_PATH + "screenshots//" + screenshotFile;
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
File finalDestination = new File(filePath);
FileUtils.copyFile(scrFile, finalDestination);
node.addScreenCaptureFromPath(filePath);
//return filePath;
}

public void reportFailure(String failureMessage, ExtentTest node) throws IOException {
node.fail(failureMessage).addScreenCaptureFromPath(takeScreenShot(failureMessage));
//takeScreenShot(failureMessage,node);
// Assert.fail(failureMessage);
}

// Select element from listbox by visible text
public void selectListBoxValue(WebElement element, String setData, ExtentTest node) throws IOException {
try {
if (isElementPresent(element, node)) {
if (demoFlag.equalsIgnoreCase("Yes"))
node.info("Selecting Listbox value. Element: " + element + ", Value: " + setData);
Select elemntType = new Select(element);
elemntType.selectByVisibleText(setData);
//elemntType.selectByValue(setData);
} else {
reportFailure("Unable to find Listbox, hence exiting test. Element: " + element, node);
driver.close();
}
} catch (Exception e) {
node.fail("Inside isElementPresent method, Exception found: " + e.getMessage());
takeScreenShot("Inside isElementPresent method", node);
}
}

public void selectListBoxOptionByIndex(WebElement element, int setData, ExtentTest node) throws IOException {
try {
if (isElementPresent(element, node)) {
if (demoFlag.equalsIgnoreCase("Yes"))
node.info("Selecting Listbox value. Element: " + element + ", Index: " + setData);
Select elemntType = new Select(element);
elemntType.selectByIndex(setData);
} else {
reportFailure("Unable to find Listbox, hence exiting test. Element: " + element, node);
driver.close();
}

} catch (Exception e) {
node.fail("Inside isElementPresent method, Exception found: " + e.getMessage());
takeScreenShot("Inside isElementPresent method", node);
}
}

// Set textbox value
public void setTextBoxValue(WebElement element, String setData, ExtentTest node)
throws WebDriverException, IOException {

try {
if (isElementPresent(element, node)) {
if (demoFlag.equalsIgnoreCase("Yes"))
node.info("Entered textbox value. Element: " + element + ", Value: " + setData);
//element.clear();
element.sendKeys(setData);
} else {
reportFailure("Unable to find Textbox, hence exiting test. Element: " + element, node);
driver.close();
}

} catch (Exception e) {
node.fail("Inside isElementPresent method, Exception found: " + e.getMessage());
takeScreenShot("Inside isElementPresent method", node);
}

}


}
 
ravs kan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
// get textbox value
public String getTextBoxValue(WebElement element, ExtentTest node) throws WebDriverException, IOException {
try {
if (isElementPresent(element, node)) {
String enteredValue = element.getAttribute("value");
if (demoFlag.equalsIgnoreCase("Yes"))
node.info("Entered value in a textbox : " + enteredValue);
return enteredValue;
} else {
reportFailure("Unable to find Textbox, hence exiting test. Element: " + element, node);
driver.close();
return null;
}

} catch (Exception e) {
node.fail("Inside isElementPresent method, Exception found: " + e.getMessage());
takeScreenShot("Inside isElementPresent method", node);
return null;
}
}

// click on element
public void clickAnElement(WebElement element, ExtentTest node) throws WebDriverException, IOException {
try {
if (isElementPresent(element, node)) {
if (demoFlag.equalsIgnoreCase("Yes"))
node.info("Clicked on Element: " + element);
element.click();
} else {
reportFailure("Unable to find Element, hence exiting test. Element: " + element, node);
driver.close();
}
} catch (Exception e) {
node.fail("Inside isElementPresent method, Exception found: " + e.getMessage());
takeScreenShot("Inside isElementPresent method", node);
}
}

public void printExecutionTime(String methodName, long startTime, long endTime, ExtentTest node) {
long time_ns = endTime - startTime;
long time_sec = TimeUnit.NANOSECONDS.toSeconds(time_ns);
long time_min = TimeUnit.NANOSECONDS.toMinutes(time_ns);
long time_hour = TimeUnit.NANOSECONDS.toHours(time_ns);

if (time_hour > 0)
node.info("\n" + methodName + " Method Execution Time: " + time_hour + " Hours, ");
if (time_min > 0)
node.info("\n" + methodName + " Method Execution Time: " + time_min % 60 + " Minutes, ");
if (time_sec > 0)
node.info("\n" + methodName + " Method Execution Time: " + time_sec % 60 + " Seconds ");
}

public WebElement createDynamicElement(String elmentxpath, String stringToReplace) {
elmentxpath = elmentxpath.replace("replace", stringToReplace);
WebElement element = driver.findElement(By.xpath(elmentxpath));
return element;
}

public void dragAndDrop(WebElement source, WebElement target, ExtentTest node)
throws InterruptedException, IOException {
try {
if (isElementPresent(source, node)) {
if (demoFlag.equalsIgnoreCase("Yes"))
node.info("Drag and drop Source Element: " + source + "Target element: " + target);
Actions actions = new Actions(driver);
actions.dragAndDrop(source, target).perform();
} else {
reportFailure("Unable to find Element, hence exiting test. Source Element: " + source
+ "Target element: " + target, node);
driver.close();
}
} catch (Exception e) {

node.fail("Inside dragAndDrop method, Exception found: " + e.getMessage());
takeScreenShot("Inside dragAndDrop method", node);
}
}


/**
* replace a string in xpath to build new xpath at runtime
*
* @param elmentxpath Xpath should contain 'replace'
* @param stringToReplace New String to be replaced in the place of 'replace' in xpath
* @return Returns new xpath as a string
*/
public String createDynamicString(String elmentxpath, String stringToReplace) {
elmentxpath = elmentxpath.replace("replace", stringToReplace);
return elmentxpath;
}

/**
* Returns child elements as list
*
* @param elementxpath         xpath of the element which identifies multiple elements
* @param node   The reporting node to add messages to
* @return                     Child elements in a list
* @throws IOException
*
* Usage Ex1: WebElement newElement = getChildItems(<xpath>).get(1) ---> returns first occurrence of the similar items
* Usage Ex2: List<WebElement> elements = getChildItems(<xpath>) ---> returns all the child items
*  
*/
public List<WebElement> getChildItems(String elementxpath, ExtentTest node) throws IOException {
try {
List<WebElement> childelements = driver.findElements(By.xpath(elementxpath));
return childelements;
}
catch(Exception e) {
node.fail("Inside isElementPresent method, Exception found: " + e.getMessage());
e.printStackTrace();
return null;
}

}
reply
    Bookmark Topic Watch Topic
  • New Topic