Question

I have the following 2 xmls:

Xml1:

<Sctn>
  <CI>
  <G>WCARD_HM</G>
  <Ty>INFO</Ty>
  <Tt>3600</Tt>
  <Ts>1395118210.1</Ts>
  </CI>
</Sctn>

XML2:

<CI>
  <G>WCARD_Off</G>
  <Ty>Test</Ty>
  <Tt>1234</Tt>
  <Ts>1395derr</Ts>
</CI>

Now, I want to add xml2 to Sctn tag of Xml1. ie, the resulting xml should be:

<Sctn>
      <CI>
        <G>WCARD_HM</G>
        <Ty>INFO</Ty>
        <Tt>3600</Tt>
        <Ts>1395118210.1</Ts>
      </CI>
      <CI>
        <G>WCARD_Off</G>
        <Ty>Test</Ty>
        <Tt>1234</Tt>
        <Ts>1395derr</Ts>
      </CI>
</Sctn>

Is there a way to do this without iterating over every element of Xml2 ?

Was it helpful?

Solution

Quite simple, actually (even with DOM standards):

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();

    Document d1 = builder.parse(...);
    Document d2 = builder.parse(...);

    d1.getDocumentElement().appendChild(d1.importNode(d2.getDocumentElement(), true));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top