-1

I have to XML Files ,say of format

<bookstore>
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="WEB">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>

Say i have 3 books category in XML one: BookCat1 BookCat2 BookCat3 & I have 4 Books Category in XML two: BookCat1 BookCat3 BookCat4 BookCat5

I have to first point out , Which categories were added or deleted ? Eg: Here BookCat2 was deleted and BookCat4 & 5 were added.

Then Later for sub elements i have to point out for every element in XML2 what sub elements were added or deleted.eg for XML2 say for BOOKCAT1 tag was added.

What would be the best way to achieve this ? I have to implement this using Java.

David
  • 141
  • 1
  • 13

3 Answers3

0

Was this your interview question? You can use XPATH //book/@category which will produce list of categories. Run XPath over both XML's can compare the results.

Dheerendra Kulkarni
  • 2,728
  • 1
  • 16
  • 18
  • Yea, but how to compare sub elements , I mean won't it be cumbersome iterating over each sub element.Is there a better way to do that ? – David Nov 19 '13 at 09:50
0

The problem to solve is to compare a tree to another, that's the same as comparing two directory structures (and their attributes) for instance.

There are libraries for that. For instance diffxml: http://diffxml.sourceforge.net/

You could also implement it yourself. Look at this article on wikipedia for Graph Isomorphism: http://en.wikipedia.org/wiki/Graph_isomorphism_problem

Of course this implies recursively traversing object graphs etc.

Also take a look at this Q&A on SO: Detect differences between tree structures

If this was your interview question they were probably interested in seeing how you would classify the problem and how you would go about solving it (reduce, abstract, corner cases, effort estimation, reuse of known solutions, trade-offs, ...).

Community
  • 1
  • 1
mwhs
  • 5,878
  • 2
  • 28
  • 34
0

You can use JAXB for this. create two classes Book and BookStore.

you need to load(unmarshal) both file xml file in java class. then using loop you can find which is added or removed.

define Book.class

@XmlRootElement(name = "book")
public class Book {
String title;

String author;

String year;

String price;

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getAuthor() {
    return author;
}

public void setAuthor(String author) {
    this.author = author;
}

public String getYear() {
    return year;
}

public void setYear(String year) {
    this.year = year;
}

public String getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price = price;
}

}

and define BookStore.class like this--

@XmlRootElement
public class BookStore {

List<Book> books;

@XmlElement(name = "book")
public List<Book> getBooks() {
    return books;
}

public void setBooks(List<Book> books) {
    this.books = books;
}

}

you can load xml file like this

 Unmarshaller um = context.createUnmarshaller();
 Bookstore bookstore2 = (BookStore) um.unmarshal(new FileReader("yourxml.xml"));
List<Book> list = bookstore2.getBooksList();
Prithvipal Singh
  • 511
  • 4
  • 9
  • 23