2014-12-31 10 views
1

我建立一個DOM文檔使用此代碼:文件似乎爲空,但我可以得到的第一個元素

private org.w3c.dom.Document createXml(WorkObject workObject, String hierarchy, String dateFormat) { 
    XMLBuilder2 builder = XMLBuilder2.create(hierarchy); 
    Map<String, Object> localVariables = workObject.getLocalVariablesMap(); 
    builder = createXmlFromMap(localVariables, builder, dateFormat); 
    Properties outputProperties = new Properties(); 
    outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "no"); 
    String xmlString = builder.asString(outputProperties); 
    LOG.debug("XML string: " + xmlString); 
    org.w3c.dom.Document xmlDoc = builder.getDocument(); 
    LOG.debug("DOM root element: " + xmlDoc.getDocumentElement().getTagName()); 
    return xmlDoc; 
} 

builder.asString()調用返回預期的XML:

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <myVar>a value</myVar> 
    <docId>GEAR-b3384419-36f8-48fc-b194-c937e7afc0dd</docId> 
</root> 

的輸出如預期的那樣,最後的LOG語句是root

但是,當我使用此代碼,將xmlDoc似乎是一個很奇怪......

傳遞xmlDoc此構造函數時:

private org.w3c.dom.Document mXmlDoc; 

public XmlMailMergeDataSet(org.w3c.dom.Document xmlDoc) { 
    mXmlDoc = xmlDoc; 
} 

在IDEA調試時,的xmlDoc價值和mXmlDoc然後是[#document: null]

不幸的是,我不能進一步調試,看看會發生什麼,然後XmlMailMergeDataSet然後傳遞給代碼不是開源的。

我在這裏錯過了一些明顯的東西嗎?

回答

1

[#document: null]並不意味着Document引用爲空,也不表示該樹爲空。這僅僅是評估xmlDoc.toString())的結果。我猜這裏的null是在這種情況下沒有意義的文檔節點的「節點值」(DocumentNode的特殊子類型)。

因此Document實例在您的代碼中似乎沒有任何問題。

+0

感謝您的澄清。這在一個方面是很好的:我的代碼似乎可以,但在另一個方面不好:我的問題似乎在我無法調試的代碼部分。 – user1145874

相關問題