2016-03-10 64 views
0

我試圖修改DOM使用由它取代一些元素的XML文檔,但我發現以下異常:拋出:DOMException在使用appendChild

03-10 10:49:20.943: W/System.err(22584): org.w3c.dom.DOMException 
03-10 10:49:20.943: W/System.err(22584): at org.apache.harmony.xml.dom.InnerNodeImpl.insertChildAt(InnerNodeImpl.java:118) 
03-10 10:49:20.943: W/System.err(22584): at org.apache.harmony.xml.dom.InnerNodeImpl.appendChild(InnerNodeImpl.java:52) 

XML文檔具有以下層次:

<?xml version="1.0" encoding="UTF-8"?> 
<msg> 
    <header> 
     <method>Call</method> 
    </header> 
</msg> 

我試圖用replaceChild()方法另一個替換元素header

doc.replaceChild(header, (Element)doc.getElementsByTagName("header").item(0)); 

但我收到上述異常。所以,我追查例外,看看它是得了拋出,導致我下面一行在org.apache.harmony.xml.dom.InnerNodeImpl類:

public Node removeChild(Node oldChild) throws DOMException { 

    LeafNodeImpl oldChildImpl = (LeafNodeImpl) oldChild; 
    if (oldChildImpl.document != document) { 
     throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, null); 
    } 

    if (oldChildImpl.parent != this) { 
     throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, null); // This is where the Exception got thrown 
    } 

    int index = oldChildImpl.index; 
    children.remove(index); 
    oldChildImpl.parent = null; 
    refreshIndices(index); 
    return oldChild; 

} 

這意味着它不能識別元素作爲一個孩子的元素該文件是不正確的,所以,我在這裏錯過了什麼?!

僅供參考,以下是我的整個方法我用了這個過程:

private void forming_and_sending_xml(String message, Element header){ 

    Document doc = null; 
    try { 
     doc = loadXMLFromString(message); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    doc.getDocumentElement().normalize(); 
    doc.replaceChild(header, (Element)doc.getElementsByTagName("header").item(0)); // this is where I got the Exception 

} 

UPDATE

我改變了替換元素,我用importNode將節點添加到文檔中,然後我將替換過程分離爲(remove - > add),這使我能夠修復與移除過程相關的所有問題,現在Element正在成功移除,但文檔不會pprove添加新的元素,並拋出上面提到的相同的異常。

我的新方法:

private void forming_and_sending_xml(String message, Element header){ 

    Document doc = null; 
    try { 
     doc = loadXMLFromString(message); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    doc.getDocumentElement().normalize(); 
    doc.importNode(header, true); 
    Element header_holder = (Element)doc.getElementsByTagName("header").item(0); 
    header_holder.getParentNode().removeChild(header_holder); // this removes the Element from the Doc succeffully 
    doc.getDocumentElement().appendChild(header); // this is where the Exception is got thrown now 

} 
+0

也許這可以幫助:WRONG_DOCUMENT_ERR:如果newChild對象是從不同的文檔而不是創建這個節點之一創建引發。如果是這種情況,您必須首先克隆/導入新元素。 – Faton

+0

@Faton我包含'importNode'並沒有新的東西,我認爲這裏的問題是必須被刪除的節點,而不是必須添加的節點。 –

+0

@Faton是正確的,我認爲,'header'元素屬於與它所替換的元素不同的文檔。你如何使用importNode()? –

回答

2

我想有兩個錯誤的位置:

  1. <header>元素必須導入到現有的文件(如已disucssed在評論) ,並且

  2. oldChild節點必須是立即子節點e上下文節點,而不是像例子中的孫子那樣。更換

    doc.replaceChild(header, (Element)doc.getElementsByTagName("header").item(0)); 
    

    doc.getDoumentElement(). 
        replaceChild(doc.importNode(header, true), 
           (Element)doc.getElementsByTagName("header").item(0)); 
    
+0

很好,那真的解決了這個問題,儘管我認爲這是我在更新中所做的事情,但儘管我得到了相同的異常。可能是因爲我必須導入它不會追加它? –

相關問題