Results 1 to 8 of 8
  1. #1
    sakhawn is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default Merge 2 different XML files in Java with similar text values

    Hi,

    I have two files to merge using Java based on a similar text identifier:

    File 1:
    <ListRecords>
    <record>
    <header>
    <identifier>oai:triple-l:2c7ba037-52a6-4323-97dd-b6ea1cdbfd18</identifier>
    <datestamp>2007-05-29T15:55:00Z</datestamp>
    <datestampasdatetime>2007-05-29T17:55:00+02:00</datestampasdatetime>
    </header>
    <metadata>
    <lom xsi:schemaLocation="some url">
    <general >
    <identifier>
    <catalog>oai</catalog>
    <entry>oai:triple-l:2c7ba037-52a6-4323-97dd-b6ea1cdbfd18</entry>
    </identifier
    <title>
    <langstring>
    <value>Graduation mw. S. de Caralt</value>
    <language>en</language>
    </langstring>
    </title>
    <catalogentry>
    <catalog>nl.wur.wurtv</catalog>
    <entry>
    <langstring>
    <value>2c7ba037-52a6-4323-97dd-b6ea1cdbfd18</value>
    <language>x-none</language>
    </langstring>
    </entry>
    </catalogentry>
    <grouplanguage>en</grouplanguage>
    <description>
    <langstring>
    <value>Sponge Culture: Learning from Biology and Ecology</value>
    <language>en</language>
    </langstring>
    </description>
    </general>
    <lifecycle xmlns="" />
    <metametadata >
    <metadatascheme>LORENET</metadatascheme>
    </metametadata>
    </lom>
    </metadata>
    </record>
    <….More Records here…..!>
    </ListRecords>

    File 2:
    <ListRecords>
    <record>
    <header>
    <identifier>some value herer</identifier>
    <datestamp>2008-07-14T09:23:25Z</datestamp>
    </header>
    <metadata>
    <group xsi:schemaLocation="some url">
    <title>User manipulating this</title>
    <feed>
    <title>My feed</title>
    <url>no.url.available</url>
    <item>
    <guid>oai:triple-l:2c7ba037-52a6-4323-97dd-b6ea1cdbfd18</guid>
    <events>
    <event>
    <dateTime>2008-03-26T13:27:49.00</dateTime>
    <action>
    <actionType>doSomeAtcion</actionType>
    </action>
    </event>
    </events>
    </item>
    </feed>
    </group>
    </metadata>
    </record>
    <....More Records here....!>
    </ListRecords>

    I want to merge <metadata> element and all its sub elements from file 1 into the file 2 within its <metadata> element based on unique text of element "<identifier>oai:triple-l:2c7ba037-52a6-4323-97dd-b6ea1cdbfd18</identifier>" in file 1and similarly <guid>oai:triple-l:2c7ba037-52a6-4323-97dd-b6ea1cdbfd18</guid> in file 2.
    There are almost 10 records to merge in a similar way..

    Any suggestions and guidelines will be highly appreciated.

    Thnx.

  2. #2
    sakhawn is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default

    I didn't got any reply from anybody :-( The task might not be clear i clarify it again.. I want to do it in XSLT...

    Both files have top elements <ListRecords> and then <record> This means that this <record> is one unique record based on <identifer> value in file1 (line 4) and <guid> value in file2 (line 14). This unique record of these similar id's in both files have different data elements i mean different fields. I want to merge this unique record of mentioned ID from file 1 into file 2.

    There is this <metadata> element in both files, file1 (line 8 to 42) and in file2 (line 7 to 26) so i want to simply copy this <metadata> element and elements in between (sub-elements) till line 43 from file 1 into file 2 after file2's <metadata> element ends at line 26 and after that last elements would be then simply be <record> and <ListRecords>
    There are 10 unique records in both files and final file should mention all of them in a similar way so i hope if one is correctly merged others follow the same template match.

    Output file might look like:
    <ListRecords>
    <record>
    <header>
    <identifier>some value herer</identifier>
    <datestamp>2008-07-14T09:23:25Z</datestamp>
    </header>
    <metadata>
    <group xsi:schemaLocation="some url">
    <title>User manipulating this</title>
    <feed>
    <title>My feed</title>
    <url>no.url.available</url>
    <item>
    <guid>oai:triple-l:2c7ba037-52a6-4323-97dd-b6ea1cdbfd18</guid>
    <events>
    <event>
    <dateTime>2008-03-26T13:27:49.00</dateTime>
    <action>
    <actionType>doSomeAtcion</actionType>
    </action>
    </event>
    </events>
    </item>
    </feed>
    </group>
    </metadata>
    <!...Metadata elements and sub elements from file 1 merged here based on unique id...>
    <metadata>
    <lom xsi:schemaLocation="some url">
    <general >
    <identifier>
    <catalog>oai</catalog>
    <entry>oai:triple-l:2c7ba037-52a6-4323-97dd-b6ea1cdbfd18</entry>
    </identifier
    <title>
    <langstring>
    <value>Graduation mw. S. de Caralt</value>
    <language>en</language>
    </langstring>
    </title>
    <catalogentry>
    <catalog>nl.wur.wurtv</catalog>
    <entry>
    <langstring>
    <value>2c7ba037-52a6-4323-97dd-b6ea1cdbfd18</value>
    <language>x-none</language>
    </langstring>
    </entry>
    </catalogentry>
    <grouplanguage>en</grouplanguage>
    <description>
    <langstring>
    <value>Sponge Culture: Learning from Biology and Ecology</value>
    <language>en</language>
    </langstring>
    </description>
    </general>
    <lifecycle xmlns="" />
    <metametadata >
    <metadatascheme>LORENET</metadatascheme>
    </metametadata>
    </lom>
    </metadata>
    <!....The data from file 1 that is copied ends here...>
    </record>
    </ListRecords>

    I hope this is clear and someone will help me and provide guidelines as i am totally new to XSLT ...

    Best Regards

    Please help me as i am really worried and first task in a new language is always such a headache

    Best Regards

  3. #3
    devunion is offline Member
    Join Date
    Jun 2008
    Posts
    56
    Rep Power
    0

    Default

    I've done similr thing with StAX XML API. May be you can use DOM in your case. Why do you want to use XSLT?
    Try Controls4J - Advanced Swing Components.

  4. #4
    sakhawn is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default

    hi,

    Well i used jdom to complete the task ...

  5. #5
    devunion is offline Member
    Join Date
    Jun 2008
    Posts
    56
    Rep Power
    0

    Default

    I've found interstting article on ibm.com. Type in google "Merge XML documents with StAX". You can use it when size of files that need to be merget is significant. Or when you need to improve performance.
    Try Controls4J - Advanced Swing Components.

  6. #6
    sakhawn is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default

    well its almost similar to what i did with Jdom, comparing the unique id's and then merging the cloned data from file 2 into file 1 and outputting it to a new file...

  7. #7
    sakhawn is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default

    now i have to transform that merged file (bascically LOM data instances) into another metadata schema (Dublin Core) and for that i definitley need XSLT and i have no experience with XSLT :-(

  8. #8
    Priyabrata is offline Member
    Join Date
    Apr 2010
    Posts
    1
    Rep Power
    0

    Default I want to I want to merge 2 xml files.

    Hi sakhawn,
    I have a simliar requirement where in i want to merge to xml file without any duplicate attribute. And it should be codded in Java.
    Seems you have the solution.
    Can you help me please?

    Thanks
    Priya

Similar Threads

  1. Merge Two Xml files ????
    By alwz_nikhil in forum XML
    Replies: 5
    Last Post: 01-18-2011, 09:18 AM
  2. Behaving text files like binary files
    By Farzaneh in forum New To Java
    Replies: 2
    Last Post: 08-27-2008, 03:20 PM
  3. Java Application Interface similar with Windows Themes
    By Heracles Choe in forum AWT / Swing
    Replies: 9
    Last Post: 05-02-2008, 03:47 AM
  4. Does OS intervene when reading Java text files
    By Tina G in forum Advanced Java
    Replies: 1
    Last Post: 04-07-2008, 02:29 PM
  5. Replies: 1
    Last Post: 03-27-2008, 05:06 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
  •