2012-06-05 57 views
1

我寫功能,必須添加節點給定節點的XML文件內。importNode拋出異常org.w3c.dom.DOMException中

下面是實現該功能的:

/** 
* Adds the node aNewChild to the end of the list of children of this node. 
* If the newChild is already in the tree, it is first removed. 
* @param aNewChild The node to add.If it is a DocumentFragment object, 
*     the entire contents of the document fragment are moved 
*     into the child list of this node 
* @return The node added or null. 
*/ 
public Node AddNode(Node aNode, Node aOldNode) { 
    Log.i(TAG, "AddNode()"); 

    if (aNode == null) { 
     Log.e(TAG, "aNode is null!"); 
     return null; 
    } 

    if (aOldNode == null) { 
     Log.e(TAG, "aOldNode is null!"); 
     return null; 
    } 

    Document document = aNode.getOwnerDocument(); 

    aOldNode = document.importNode(aOldNode, true); 

    return aNode.appendChild(aOldNode); 

} /* Node AddNode(Node aNode, Node aOldNode) **/ 

沒有問題,此代碼運行於Android 4.0.3大,功能添加節點到給定節點沒有錯誤,但是當我的Android 2.3下運行相同的代碼上線aOldNode = document.importNode(aOldNode, true); 0.3功能崩潰,當我嘗試添加try catch塊這樣的:

try { 
    aOldNode = document.importNode(aOldNode, true); 
} 
catch(Exception ex) { 
    Log.e(TAG, ex.getMessage()); 
} 

我看到錯誤消息只null代替。誰知道原因是什麼?

這裏是例外:

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):  at org.apache.harmony.xml.dom.NodeImpl.setNameNS(NodeImpl.java:227) 
06-05 17:58:09.122: I/TestRunner(2769):  at org.apache.harmony.xml.dom.ElementImpl.<init>(ElementImpl.java:50) 
06-05 17:58:09.122: I/TestRunner(2769):  at org.apache.harmony.xml.dom.DocumentImpl.createElementNS(DocumentImpl.java:336) 
06-05 17:58:09.122: I/TestRunner(2769):  at org.apache.harmony.xml.dom.DocumentImpl.shallowCopy(DocumentImpl.java:156) 
06-05 17:58:09.122: I/TestRunner(2769):  at org.apache.harmony.xml.dom.DocumentImpl.cloneOrImportNode(DocumentImpl.java:208) 
06-05 17:58:09.122: I/TestRunner(2769):  at org.apache.harmony.xml.dom.DocumentImpl.importNode(DocumentImpl.java:222) 
06-05 17:58:09.122: I/TestRunner(2769):  at com.fido.android.framework.service.XMLDOMNode.AddNode(XMLDOMNode.java:108) 
06-05 17:58:09.122: I/TestRunner(2769):  at com.fido.android.test.framework.service.XMLDOMNodeTest.testAddNodeNodeNode(XMLDOMNodeTest.java:89) 
06-05 17:58:09.122: I/TestRunner(2769):  at java.lang.reflect.Method.invokeNative(Native Method) 
06-05 17:58:09.122: I/TestRunner(2769):  at java.lang.reflect.Method.invoke(Method.java:507) 
06-05 17:58:09.122: I/TestRunner(2769):  at junit.framework.TestCase.runTest(TestCase.java:154) 
06-05 17:58:09.122: I/TestRunner(2769):  at junit.framework.TestCase.runBare(TestCase.java:127) 
06-05 17:58:09.122: I/TestRunner(2769):  at junit.framework.TestResult$1.protect(TestResult.java:106) 
06-05 17:58:09.122: I/TestRunner(2769):  at junit.framework.TestResult.runProtected(TestResult.java:124) 
06-05 17:58:09.122: I/TestRunner(2769):  at junit.framework.TestResult.run(TestResult.java:109) 
06-05 17:58:09.122: I/TestRunner(2769):  at junit.framework.TestCase.run(TestCase.java:118) 
06-05 17:58:09.122: I/TestRunner(2769):  at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169) 
06-05 17:58:09.122: I/TestRunner(2769):  at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154) 
06-05 17:58:09.122: I/TestRunner(2769):  at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529) 
06-05 17:58:09.122: I/TestRunner(2769):  at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448) 
06-05 17:58:09.132: I/TestRunner(2769): ----- end exception ----- 

至於我能理解importNode是一個越野車的功能,還有什麼我可以用它來添加節點的節點?

回答

0

我找到解決方案,它爲我工作,在這裏被修改功能,添加節點到節點不拋出任何異常:

/** 
    * Adds the node aNewChild to the end of the list of children of this node. 
    * If the newChild is already in the tree, it is first removed. 
    * @param aNewChild The node to add.If it is a DocumentFragment object, 
    *     the entire contents of the document fragment are moved 
    *     into the child list of this node 
    * @return The node added or null. 
    */ 
    public Node AddNode(Node aNode, Node aOldNode) { 
     CustomLog.i(TAG, "AddNode()"); 

     if (aNode == null) { 
      CustomLog.e(TAG, "aNode is null!"); 
      return null; 
     } 

     if (aOldNode == null) { 
      CustomLog.e(TAG, "aOldNode is null!"); 
      return null; 
     } 

     Document document = aNode.getOwnerDocument(); 

     aOldNode = document.adoptNode(aOldNode); 

     return aNode.appendChild(aOldNode); 

    } /* Node AddNode(Node aNode, Node aOldNode) **/ 

我只是用aOldNode = document.adoptNode(aOldNode);代替aOldNode = document.importNode(aOldNode, true);方法。