W3C home > Mailing lists > Public > www-dom@w3.org > October to December 1999

help on DOM programming

From: Jian Chen <jchen@smartcad4.me.wisc.edu>
Date: Thu, 16 Dec 1999 17:30:57 -0600
Message-ID: <38597631.E93D0627@smartcad.me.wisc.edu>
To: www-dom@w3.org
Dear Sir,

I have a  problem on DOM programming, If you have time,please read my
story.

I want to copy a fragment of document to another document. The fragment
is:

<Part num="1">
      <Part_Name>uuu</Part_Name>
      <Part_Model_Number>1234</Part_Model_Number>
      <Manufacturer>yyy</Manufacturer>
      <Specifications Parameter1="123" Parameter2="456"
Parameter3="789"/>
   </Part>

  <Part num="2">
      <Part_Name>vvv</Part_Name>
      <Part_Model_Number>5678</Part_Model_Number>
      <Manufacturer>ppp</Manufacturer>
      <Specifications Parameter1="123" Parameter2="456"
Parameter3="789"/>
   </Part>

I use the following JAVA code:

   NodeList NL = resultDoc1.getElementsByTagName("Part");
   DocumentFragment DFrag1 = resultDoc1.createDocumentFragment();

   Document resultDoc2 = DOMUtil.createDocument();
   DocumentFragment DFrag2 = resultDoc2.createDocumentFragment();
   Element root2 = resultDoc2.createElement("ROWSET");
   resultDoc2.appendChild( root2 );

  int k;
  for (k=0;k<NL.getLength();k++){

   try{
     Element row_ele = resultDoc2.createElement("ROW");

     //the error line 1
     DFrag1 = (DocumentFragment)NL.item(k);
     DFrag2 = (DocumentFragment)DFrag1.cloneNode(true);

    //the error line 2
     row_ele.appendChild(DFrag2);
     root2.appendChild(row_ele);
    }
    catch (DOMException e) {
      e.printStackTrace();
    }
  }

But I got the java.lang.ClassCastException on the line which I indicate
"the error line 1" above.

If I just use Node type, instead of DocumentFragment, then I got the
com.ibm.xml.dom.DOMExceptionImpl on the line which I indicate "the error

line 2" above.

Would you give me some suggestion?
Received on Thursday, 16 December 1999 18:27:52 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Friday, 22 June 2012 06:13:47 GMT