Merging XML

Discussion in 'Java' started by Ravi Shankar, Oct 12, 2005.

  1. Ravi Shankar

    Ravi Shankar Guest

    HI all,

    I would like to merge two or more XML documents. But we need to make sure
    that merging is not appending. All common tags should be at one place etc
    etc. ANyone has written something similar? Any open source for this?

    Thanks and regards,
    Ravi
     
    Ravi Shankar, Oct 12, 2005
    #1
    1. Advertising

  2. On Thu, 13 Oct 2005 05:36:34 +0800, Ravi Shankar wrote:

    > HI all,
    >
    > I would like to merge two or more XML documents. But we need to make sure
    > that merging is not appending. All common tags should be at one place etc
    > etc. ANyone has written something similar? Any open source for this?


    What do you mean, merge two XML documents? Suppose you have

    <root>
    <achild anattribute="foo" anotherattribute="bar"/>
    <achild>
    <agrandchild nothing="happens"/>
    </achild>
    </root>

    and

    <ca:calculation xmlns:ca="http://xxxxxxxxx.invalid/calculate">
    <ca:add>
    <ca:constant value="7.9"/>
    <ca:sub>
    <ca:constant value="8.1"/>
    <ca:constant value="3.8"/>
    </ca:sub>
    </ca:add>
    </ca:calculation>

    What would the merged document look like?

    --
    You can't run away forever,
    But there's nothing wrong with getting a good head start.
    --- Jim Steinman, "Rock and Roll Dreams Come Through"
     
    Stefan Schulz, Oct 13, 2005
    #2
    1. Advertising

  3. Ravi Shankar

    Ravi Shankar Guest

    Dear Stefan,

    In the examples you had used, it will be just appending.

    To make it clear, let me illustrate with another.
    1) Input1.xml

    <?xml version="1.0"?>
    <app name="testapp" lifetime="900">
    <mainmenu>
    <menu id="1" caption="test"/>
    <menu id="2" caption="another test"/>
    </mainmenu>
    <forms>
    <testform autosize="1"/>
    <testform2 autosize="0"/>
    </forms>
    </app>

    2) Input2.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <app lifetime="100">
    <mainmenu>
    <menu id="2" caption="my test"/>
    <menu id="3" caption="my menu"/>
    </mainmenu>
    <forms>
    <testform2 autosize="1"/>
    </forms>
    </app>

    3) Result.xml should be

    <?xml version="1.0" encoding="UTF-8"?>
    <app name="testapp" lifetime="100">
    <mainmenu>
    <menu id="1" caption="test"/>
    <menu id="2" caption="my test"/>
    <menu id="3" caption="my menu"/>
    </mainmenu>
    <forms>
    <testform autosize="1"/>
    <testform2 autosize="1"/>
    </forms>
    </app>


    WHat I request is an answer, not specific to above example, but generic. I
    mean, without hardcoding any tag names, I would like to generate it
    dynamically. Also, it is possible that number of input XMLs to be merged may
    not be two.

    Hence to put it briefly, generate an XML file, by merging n different XML
    files, where 2<= n <= 25

    Please help, thanks

    Warmest regards,
    Ravi


    "Stefan Schulz" <> wrote in message
    news:p...
    > On Thu, 13 Oct 2005 05:36:34 +0800, Ravi Shankar wrote:
    >
    >> HI all,
    >>
    >> I would like to merge two or more XML documents. But we need to make sure
    >> that merging is not appending. All common tags should be at one place etc
    >> etc. ANyone has written something similar? Any open source for this?

    >
    > What do you mean, merge two XML documents? Suppose you have
    >
    > <root>
    > <achild anattribute="foo" anotherattribute="bar"/>
    > <achild>
    > <agrandchild nothing="happens"/>
    > </achild>
    > </root>
    >
    > and
    >
    > <ca:calculation xmlns:ca="http://xxxxxxxxx.invalid/calculate">
    > <ca:add>
    > <ca:constant value="7.9"/>
    > <ca:sub>
    > <ca:constant value="8.1"/>
    > <ca:constant value="3.8"/>
    > </ca:sub>
    > </ca:add>
    > </ca:calculation>
    >
    > What would the merged document look like?
    >
    > --
    > You can't run away forever,
    > But there's nothing wrong with getting a good head start.
    > --- Jim Steinman, "Rock and Roll Dreams Come Through"
    >
    >
     
    Ravi Shankar, Oct 13, 2005
    #3
  4. On Thu, 13 Oct 2005 20:38:31 +0800, Ravi Shankar wrote:

    > Dear Stefan,
    >
    > In the examples you had used, it will be just appending.


    You still need to specify a number of things:

    * What happens when the root elements mismatch?
    * What happens if there are common subtrees A and B, but one
    of the documents has a node C between these nodes?

    Once you specify this, i can try coming up with an idea how to solve the
    problem. I will not give you a complete implementation, however. :)

    --
    You can't run away forever,
    But there's nothing wrong with getting a good head start.
    --- Jim Steinman, "Rock and Roll Dreams Come Through"
     
    Stefan Schulz, Oct 13, 2005
    #4
  5. Aleksi Kallio, Oct 13, 2005
    #5
  6. Ravi Shankar

    Ravi Shankar Guest

    Dear Stefan,

    Thanks for reply.

    My answers:-

    1) Assume roots will never be different
    2) If one of the document has a node C, then that will be copied as it is to
    destination

    Best regards,
    Ravi

    "Stefan Schulz" <> wrote in message
    news:p...
    > On Thu, 13 Oct 2005 20:38:31 +0800, Ravi Shankar wrote:
    >
    >> Dear Stefan,
    >>
    >> In the examples you had used, it will be just appending.

    >
    > You still need to specify a number of things:
    >
    > * What happens when the root elements mismatch?
    > * What happens if there are common subtrees A and B, but one
    > of the documents has a node C between these nodes?
    >
    > Once you specify this, i can try coming up with an idea how to solve the
    > problem. I will not give you a complete implementation, however. :)
    >
    > --
    > You can't run away forever,
    > But there's nothing wrong with getting a good head start.
    > --- Jim Steinman, "Rock and Roll Dreams Come Through"
    >
    >
     
    Ravi Shankar, Oct 13, 2005
    #6
  7. On Fri, 14 Oct 2005 03:41:14 +0800, Ravi Shankar wrote:

    > 1) Assume roots will never be different
    > 2) If one of the document has a node C, then that will be copied as it is to
    > destination


    Hmm, this is a problem that requires a significant amount of work. Look
    into text diff algorithms. If you process nodes instead of lines, you
    should be able to handle a single node much like a document. Recurse for
    matching nodes.

    --
    You can't run away forever,
    But there's nothing wrong with getting a good head start.
    --- Jim Steinman, "Rock and Roll Dreams Come Through"
     
    Stefan Schulz, Oct 14, 2005
    #7
  8. Ravi Shankar

    Ravi Shankar Guest

    Dear all,

    The problem had been closed. With some diifculty, I could write a 240 line
    XSL stylesheet which accomplished the task beautifully. Now it is in our
    production and is live :)

    I have written it such that if we set a parameter "replace", then it will
    function as my requirement. If you give parameter "merge", it will simply
    merge the sources. If instead you give "with" followed by a node name, then
    that won't include that node in operation at all. Kudos to XSLT.

    Best regards,
    Ravi
    "Stefan Schulz" <> wrote in message
    news:p...
    > On Fri, 14 Oct 2005 03:41:14 +0800, Ravi Shankar wrote:
    >
    >> 1) Assume roots will never be different
    >> 2) If one of the document has a node C, then that will be copied as it is
    >> to
    >> destination

    >
    > Hmm, this is a problem that requires a significant amount of work. Look
    > into text diff algorithms. If you process nodes instead of lines, you
    > should be able to handle a single node much like a document. Recurse for
    > matching nodes.
    >
    > --
    > You can't run away forever,
    > But there's nothing wrong with getting a good head start.
    > --- Jim Steinman, "Rock and Roll Dreams Come Through"
    >
    >
     
    Ravi Shankar, Oct 14, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mael Guillemot

    merging 2 xml into 1

    Mael Guillemot, Jul 4, 2003, in forum: XML
    Replies:
    1
    Views:
    1,634
    Klaus Johannes Rusch
    Jul 4, 2003
  2. Joe
    Replies:
    0
    Views:
    423
  3. Yannick Patois

    Merging two XML tree: xslt or dom ?

    Yannick Patois, Jul 20, 2004, in forum: XML
    Replies:
    1
    Views:
    954
    Uche Ogbuji
    Jul 23, 2004
  4. Ked
    Replies:
    0
    Views:
    398
  5. Replies:
    2
    Views:
    17,421
    Philippe Poulard
    Aug 24, 2006
Loading...

Share This Page