Results 1 to 6 of 6

Thread: Merging XML

  1. #1
    KEVINR is offline Member
    Join Date
    Jan 2009
    Posts
    3
    Rep Power
    0

    Default Merging XML

    How do I merge the following XML files?

    file#1

    <param-data>
    <paramdb-record>
    <UUID>40288014109c474101109c484d5a03f5</UUID>
    <PRODUCT>3X</PRODUCT>
    <PROC_EQUIP_TYPE>ECO</PROC_EQUIP_TYPE>
    <PROC_STAGE>SHLD_VP</PROC_STAGE>
    <PROC_STEP>CLEAN_ECOSNOW</PROC_STEP>
    <STATE>ACTIVATED</STATE>
    <REVISION>0</REVISION>
    <TIMESTAMP>2006-12-14 13:09:42</TIMESTAMP>
    <DATA><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
    <data><ENABLE>true</ENABLE><INTERVAL><SAMPLING_RATE>2</SAMPLING_RATE></INTERVAL><PAUSE_SETTINGS><MAX_TIME><TIME_IN_DAYS>7 </TIME_IN_DAYS><LOTS_TO_RESTART>1</LOTS_TO_RESTART></MAX_TIME></PAUSE_SETTINGS><GENERAL_SETTINGS><ENABLE_WEEKEND>t rue</ENABLE_WEEKEND></GENERAL_SETTINGS></data>]]></DATA>
    </paramdb-record>
    <paramdb-record>
    <UUID>40288014109c474101109c484e5403f6</UUID>
    <PRODUCT>5C</PRODUCT>
    <PROC_EQUIP_TYPE>ECO</PROC_EQUIP_TYPE>
    <PROC_STAGE>SHLD_VP</PROC_STAGE>
    <PROC_STEP>CLEAN_ECOSNOW</PROC_STEP>
    <STATE>ACTIVATED</STATE>
    <REVISION>0</REVISION>
    <TIMESTAMP>2006-12-14 13:10:58</TIMESTAMP>
    <DATA><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
    <data><ENABLE>true</ENABLE><INTERVAL><SAMPLING_RATE>2</SAMPLING_RATE></INTERVAL><PAUSE_SETTINGS><MAX_TIME><TIME_IN_DAYS>7 </TIME_IN_DAYS><LOTS_TO_RESTART>1</LOTS_TO_RESTART></MAX_TIME></PAUSE_SETTINGS><GENERAL_SETTINGS><ENABLE_WEEKEND>t rue</ENABLE_WEEKEND></GENERAL_SETTINGS></data>]]></DATA>

    File#2

    <param-data>
    <paramdb-record>
    <UUID>40288014109c474101109c48455b03e4</UUID>
    <MET_EQUIP_TYPE>AFM</MET_EQUIP_TYPE>
    <MET_STAGE>SHLD_VM</MET_STAGE>
    <MET_STEP>MEAS_AFM</MET_STEP>
    <PROC_EQUIP_TYPE>ECO</PROC_EQUIP_TYPE>
    <PROC_STAGE>SHLD_VP</PROC_STAGE>
    <PROC_STEP>CLEAN_ECOSNOW</PROC_STEP>
    <STATE>ACTIVATED</STATE>
    <REVISION>0</REVISION>
    <TIMESTAMP>2006-12-12 18:34:33</TIMESTAMP>
    <DATA><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
    <data/>]]></DATA>
    </paramdb-record>
    <paramdb-record>
    <UUID>40288014109c474101109c48465503e5</UUID>
    <MET_EQUIP_TYPE>SEM</MET_EQUIP_TYPE>
    <MET_STAGE>WP1_PH</MET_STAGE>
    <MET_STEP>MEAS_CD_SEM</MET_STEP>
    <PROC_EQUIP_TYPE>ASM</PROC_EQUIP_TYPE>
    <PROC_STAGE>WP1_PH</PROC_STAGE>
    <PROC_STEP>COAT_EXP_DEV</PROC_STEP>
    <STATE>ACTIVATED</STATE>
    <REVISION>0</REVISION>
    <TIMESTAMP>2006-05-10 12:19:34</TIMESTAMP>
    <DATA><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
    <data/>]]></DATA>


    The resultant file should contain all of the info from file#1 and only the MET_* from file#2 where the PROC_* paramters are equal.

    Thanks in advance for any help

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    I can think of several ways, depending on how heavily you want to depend on XML.

    The challenge seems to be that you want to take each file 1 record and merge tags into file 1 from file 2, based on matching criteria.

    What if multiple file 2 records match a file 1 record? Should all the tags be included in file 1's record?

    I suggest looking at JDOM and XPATH for possible XML-based solutions.

    Another option is to use either lists of objects or database records. Read all the file 1 records into a list of object 1 (or record 1), do the same with file 2 records. For each object 1, compare it to all the object 2. If you find a match, include the fields from object 2 into object 1.

  3. #3
    KEVINR is offline Member
    Join Date
    Jan 2009
    Posts
    3
    Rep Power
    0

    Default Merging XML

    Thanks Steve,
    You are correct........I need to insert the MET_* fields from file #1 into file #2where the PROC_* are equal. They also need to be inserted after PROC_STEP.

    The step I'm having trouble with is the Java code to read each record from file#1 and look up the MET_* data from file#2 and creating file#3 with the merged data in the correct order.

    Again thanks for your help.

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    I poked around last night, and XPATH is implemented with DOM, not with JDOM. I've worked with XML in Java, but not with XPATH. However, I believe you can accomplish what you are trying with DOM and XPATH.

    Having said that, there is a fairly steep learning curve with XML processing in general. If you have time, wade through it.

    If not, just parse the files and create objects. JDOM is built for that. Once you have objects, forget about XML for the moment and join the data using loops and straight Java code, adding the fields from the one object to the other.

    At the end of all this, you will have a set of objects with the necessary information. Write the contents of each object out as an XML document.

  5. #5
    KEVINR is offline Member
    Join Date
    Jan 2009
    Posts
    3
    Rep Power
    0

    Default Merging XML

    Steve thanks again!

    I am editing existing Java code and have limited Java experience. The part I'm missing is the Java code to join the data using loops and adding the fields from the one object to the other.

    Any help would be appreciated.

  6. #6
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Create a class for each file. Add fields to each class to hold the values of every field in the file, including any fields you want to add. Create getters and setters for each field.

    In the class you want to pull fields from, create a method
    [CODE}
    public boolean matches(final String pField1, final String pField2, ...) {
    boolean match = false;
    // Compare the parameters to the fields in the class, set match to true if they match
    return match;
    }
    [CODE]
    In your main method, you will need to ArrayList's, one to hold each type of object. Create objects of each type from your XML files and put them in the appropriate ArrayList.

    Once you have loaded the information from both XML files into objects,
    Java Code:
    final ArrayList<Class1> class1List = new ArrayList<Class1>();
    final ArrayList<Class2> class2List = new ArrayList<Class2>();
    // Create objects and add them to the lists
    for (Class1 class1: class1List) {
      for (Class2 class2: class2List) {
        if (class2.matches(class1.getField1(), class1.getField2(), ...)) {
          class1.setFieldX(class2.getFieldX());
          class1.setFieldY(class2.getFieldY());
          ....
          break; // if only one class2 matches each class1
      }
    }
    Note that if more than one class2 matches a class1, the above will need to be made more sophisticated, to hold multiple sets of values from class2.

Similar Threads

  1. merging selected table cells using javascript
    By Renjini in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 06-17-2008, 01:04 AM
  2. Merging Ideas
    By CompleteBeginner in forum New To Java
    Replies: 1
    Last Post: 05-19-2008, 02:15 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
  •