如何拼接两结构相同的XML文档

在工作中遇到这样的问题:
在主表单中用户可以添加5项产品
多余的产品必须打开另外一张表单以附件形式添加
最后保存的时候将主表单和附件表单的数据一起提交,以下是我的实现方式:
  var xmlAccessories = createDomDocument();
  var xmlInsertDoc = createDomDocument();

                           xmlAccessories.loadXML(“<DETAILS>
                                                                         <TABLE>
                                                                                <Res>6</Res>
                                                                                <Type>6</Type>
                                                                                <Magnitude>6</Magnitude>
                                                                                <Budget>6</Budget>
                                                                                <Price>36</Price>
                                                                                <Date>2009-09-06</Date>
                                                                         </TABLE>
                                                                         <TABLE>
                                                                                <Res>7</Res>
                                                                                <Type>7</Type>
                                                                                <Magnitude>7</Magnitude>
                                                                                <Budget>7</Budget>
                                                                                <Price>49</Price>
                                                                                <Date>2009-09-07</Date>
                                                                         </TABLE>
                                                                  </DETAILS>”);
  xmlInsertDoc.loadXML( "<DETAILS>
                                     <TABLE>
                                            <Res>1</Res>
                                            <Type>1</Type>
                                            <Magnitude>1</Magnitude>
                                            <Budget>1</Budget>
                                            <Price>1</Price>
                                            <Date>2009-09-01</Date>
                                     </TABLE>      
                                     <TABLE>
                                           <Res>2</Res>
                                            <Type>2</Type>
                                            <Magnitude>2</Magnitude>
                                            <Budget>2</Budget>
                                            <Price>4</Price>
                                            <Date>2009-09-02</Date>
                                     </TABLE>
                                     <TABLE>
                                            <Res>3</Res>
                                            <Type>3</Type>
                                            <Magnitude>3</Magnitude>
                                            <Budget>3</Budget>
                                            <Price>9</Price>
                                            <Date>2009-09-03</Date>
                                     </TABLE>
                                     <TABLE>
                                            <Res>4</Res>
                                            <Type>4</Type>
                                            <Magnitude>4</Magnitude>
                                            <Budget>4</Budget>
                                            <Price>16</Price>
                                            <Date>2009-09-04</Date>
                                     </TABLE>
                                     <TABLE>
                                            <Res>5</Res>
                                            <Type>5</Type>
                                            <Magnitude>5</Magnitude>
                                            <Budget>5</Budget>
                                            <Price>25</Price>
                                            <Date>2009-09-05</Date>
                               </TABLE>
                  </DETAILS>");
  var nodeAccess =  xmlAccessories.documentElement;
  var nodeDetails = xmlInsertDoc.documentElement.selectSingleNode("DETAILS");
  //var nodeTable = nodeAccess.firstChild;
  //while(nodeTable!=null)
  //{
  // nodeDetails.appendChild(nodeTable.cloneNode(true));
  // nodeTable=nodeTable.nextSibling;
  //}
  
  for(var i=nodeAccess.childNodes.length-1;i>=0;i--)
  {
   var nodeTable = nodeAccess.childNodes[i];
   nodeDetails.appendChild(nodeTable);
  }

你可能感兴趣的:(xml)