Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java importNode抛出异常org.w3c.dom.domeException_Java_Android_Xml_Nodes - Fatal编程技术网

Java importNode抛出异常org.w3c.dom.domeException

Java importNode抛出异常org.w3c.dom.domeException,java,android,xml,nodes,Java,Android,Xml,Nodes,我正在编写一个函数,该函数必须将节点添加到XML文档中的给定节点 下面是该功能的实现: /** * 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 DocumentFragmen

我正在编写一个函数,该函数必须将节点添加到XML文档中的给定节点

下面是该功能的实现:

/**
 * 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上运行得很好,没有问题,函数add node到给定的节点没有错误,但是当我在Android 2.3.3下运行相同的代码时,函数aOldNode=document.importNodeaOldNode,true;当我尝试像这样添加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 -----
/**
  * 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) **/

据我所知,importNode是一个有缺陷的函数,我还可以使用什么来将节点添加到节点?

我找到了解决方案,它对我有效,下面是一个修改过的函数,它可以将节点添加到节点而不引发任何异常:

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 -----
/**
  * 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.adaptenodeaoldnode;不是aOldNode=document.importNodeaOldNode,而是true;方法