Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By gimbal2

Thread: Compare XML in Java

  1. #1
    SmittyWerben is offline Member
    Join Date
    Aug 2013
    Posts
    8
    Rep Power
    0

    Default Compare XML in Java

    Hello to everyone,

    I am pretty new to Java and my task is to compare two very similar, but not identical, .xml files in a Java programm.
    I am looking for a simple solution, because I actually only need to point out the differences of the files in kind of a changelog.
    Is this possible and how?

    I'd appreciate every helpful answer,
    thanks.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Compare XML in Java

    Anything is possible, but you need to have the specifications crystal clear. What you're telling now is "I need something like this".

    Point out the differences how? You need to log which lines are different, or which specific characters in a line are different, or is it on the level of XML nodes (as in "xml A contains node X and xml B does not")?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    SmittyWerben is offline Member
    Join Date
    Aug 2013
    Posts
    8
    Rep Power
    0

    Default Re: Compare XML in Java

    Thanks for the quick reply!

    Since XML B ist just a later version of XML A, there are just really small differences.
    XML nodes normally won't change, so it's basically about the lines.
    I need a log with the differences that happend within lines, not within nodes, which should make it easier, shouldn't it?

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Compare XML in Java

    Yes because it doesn't matter that it is XML, you can just treat it as generic text data. So basically you would have to loop through the lines (with a BufferedReader for example) and see if they are equal or not.

    IF the situation is as easy as you sketch it. It usually isn't, but that is something you figure out when you put it in production.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    SmittyWerben is offline Member
    Join Date
    Aug 2013
    Posts
    8
    Rep Power
    0

    Default Re: Compare XML in Java

    Quote Originally Posted by gimbal2 View Post
    Yes because it doesn't matter that it is XML, you can just treat it as generic text data. So basically you would have to loop through the lines (with a BufferedReader for example) and see if they are equal or not.
    To be honest, I do not even know how to do this.
    How could I read the XMLs and compare the lines in Code?

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Compare XML in Java

    Get the Java book that you have to learn the language and read the chapter on file I/O.
    SmittyWerben likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    SmittyWerben is offline Member
    Join Date
    Aug 2013
    Posts
    8
    Rep Power
    0

    Default Re: Compare XML in Java

    Quote Originally Posted by gimbal2 View Post
    Get the Java book that you have to learn the language and read the chapter on file I/O.
    Ok, thank you for your help!

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Compare XML in Java

    And to supplement what Gimbal said, check out the Oracle tutorials. There is a link in my signature:

    Regards,
    Jim

    |
    |
    |
    |
    |
    V
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Compare XML in Java

    he he :) Well you are being nice and my response was a douchy yet honestly intended "read stuff to know how stuff works" response ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 7
    Last Post: 04-11-2013, 09:48 AM
  2. Java code to compare 2 PDF files
    By Shanthuday in forum Forum Lobby
    Replies: 2
    Last Post: 01-08-2013, 11:55 AM
  3. Replies: 4
    Last Post: 01-03-2013, 05:55 PM
  4. Compare two resultsets and find the difference in java
    By prachib in forum Advanced Java
    Replies: 6
    Last Post: 11-28-2012, 03:34 PM
  5. compare sensorvalue leJOS, java for LEGO NXT
    By reinout91 in forum New To Java
    Replies: 0
    Last Post: 06-21-2011, 11:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •