java - importNode throws exception org.w3c.dom.DOMException -


i writing function must add node given node in xml document.

here implementation of function:

/**  * adds node anewchild end of list of children of node.  * if newchild in tree, first removed.  * @param anewchild node add.if documentfragment object,  *                  entire contents of document fragment moved  *                  child list of node  * @return node added or null.  */  public node addnode(node anode, node aoldnode) {     log.i(tag, "addnode()" );      if (anode == null) {         log.e(tag, "anode null!");         return null;     }      if (aoldnode == null) {         log.e(tag, "aoldnode null!");         return null;     }      document document = anode.getownerdocument();      aoldnode = document.importnode(aoldnode, true);      return anode.appendchild(aoldnode);  } /* node addnode(node anode, node aoldnode) **/ 

this code run great on android 4.0.3 no problems, function add node given node without errors, when run same code under android 2.3.3 function crashes on line aoldnode = document.importnode(aoldnode, true); when try add try catch block this:

try {     aoldnode = document.importnode(aoldnode, true); } catch(exception ex) {     log.e(tag, ex.getmessage()); } 

i see null instead of error message. know whats reason ?

here exception:

06-05 17:58:09.111: i/testrunner(2769): ----- begin exception -----  06-05 17:58:09.122: i/testrunner(2769): org.w3c.dom.domexception  06-05 17:58:09.122: i/testrunner(2769):     @ org.apache.harmony.xml.dom.nodeimpl.setnamens(nodeimpl.java:227)  06-05 17:58:09.122: i/testrunner(2769):     @ org.apache.harmony.xml.dom.elementimpl.<init>(elementimpl.java:50)  06-05 17:58:09.122: i/testrunner(2769):     @ org.apache.harmony.xml.dom.documentimpl.createelementns(documentimpl.java:336)  06-05 17:58:09.122: i/testrunner(2769):     @ org.apache.harmony.xml.dom.documentimpl.shallowcopy(documentimpl.java:156)  06-05 17:58:09.122: i/testrunner(2769):     @ org.apache.harmony.xml.dom.documentimpl.cloneorimportnode(documentimpl.java:208)  06-05 17:58:09.122: i/testrunner(2769):     @ org.apache.harmony.xml.dom.documentimpl.importnode(documentimpl.java:222)  06-05 17:58:09.122: i/testrunner(2769):     @ com.fido.android.framework.service.xmldomnode.addnode(xmldomnode.java:108)  06-05 17:58:09.122: i/testrunner(2769):     @ com.fido.android.test.framework.service.xmldomnodetest.testaddnodenodenode(xmldomnodetest.java:89)  06-05 17:58:09.122: i/testrunner(2769):     @ java.lang.reflect.method.invokenative(native method)  06-05 17:58:09.122: i/testrunner(2769):     @ java.lang.reflect.method.invoke(method.java:507)  06-05 17:58:09.122: i/testrunner(2769):     @ junit.framework.testcase.runtest(testcase.java:154)  06-05 17:58:09.122: i/testrunner(2769):     @ junit.framework.testcase.runbare(testcase.java:127)  06-05 17:58:09.122: i/testrunner(2769):     @ junit.framework.testresult$1.protect(testresult.java:106)  06-05 17:58:09.122: i/testrunner(2769):     @ junit.framework.testresult.runprotected(testresult.java:124)  06-05 17:58:09.122: i/testrunner(2769):     @ junit.framework.testresult.run(testresult.java:109)  06-05 17:58:09.122: i/testrunner(2769):     @ junit.framework.testcase.run(testcase.java:118)  06-05 17:58:09.122: i/testrunner(2769):     @ android.test.androidtestrunner.runtest(androidtestrunner.java:169)  06-05 17:58:09.122: i/testrunner(2769):     @ android.test.androidtestrunner.runtest(androidtestrunner.java:154)  06-05 17:58:09.122: i/testrunner(2769):     @ android.test.instrumentationtestrunner.onstart(instrumentationtestrunner.java:529)  06-05 17:58:09.122: i/testrunner(2769):     @ android.app.instrumentation$instrumentationthread.run(instrumentation.java:1448)  06-05 17:58:09.132: i/testrunner(2769): ----- end exception ----- 

as can understand importnode buggy function, else can use add node node ?

i found solution , works me, here modified function add node node without throwing exception:

/**   * adds node anewchild end of list of children of node.   * if newchild in tree, first removed.   * @param anewchild node add.if documentfragment object,   *                  entire contents of document fragment moved   *                  child list of node   * @return node added or null.   */   public node addnode(node anode, node aoldnode) {         customlog.i(tag, "addnode()" );          if (anode == null) {             customlog.e(tag, "anode null!");             return null;         }          if (aoldnode == null) {             customlog.e(tag, "aoldnode null!");             return null;         }          document document = anode.getownerdocument();          aoldnode = document.adoptnode(aoldnode);          return anode.appendchild(aoldnode);      } /* node addnode(node anode, node aoldnode) **/ 

i use aoldnode = document.adoptnode(aoldnode); instead of aoldnode = document.importnode(aoldnode, true); method.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -