2012-08-08 31 views
1

我正在使用Java中的簡單XML解析器。我一直使用很多源成功使用DocumentBuilderFactory,但我的新增功能之一是單個節點的集合。具有多個父節點的DocumentBuilderFactory

的file.xml看起來是這樣的:

<XML Version....> 
<!DOCTYPE...> 
<main_document_node> 
.....others.... 
</main_document_node> 
<XML Version....> 
<!DOCTYPE...> 
<main_document_node> 
.....others.... 
</main_document_node> 

我一直在使用這樣的命令:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
Document d = dbf.newDocumentBuilder().parse(/path/file.xml); 

但是,這似乎並沒有與多個父節點工作。有沒有比編寫一個臨時工作文件更簡單的方法,我寫了一個main_document_node?這僞代碼將是

new Writer temp.xml 
new Reader file.xml 
while not at the end of file.xml 
    read/write first main_document_node to temp 
    parse temp.xml 

我認爲應該有使用的DocumentBuilderFactory的的InputSource /流選擇的方式,但我不知道如何去這樣做。

謝謝!

回答

1

您可以跳過創建從computet字符串片段的InputStream(一個節點),以文件步驟寫:

new Reader file.xml 
while not at the end of file.xml { 
    String node = read_first_main_document_node(); 
    InputStream is = new ByteArrayInputStream(node.getBytes(charset)); 
    parse(is); 
} 
+0

感謝。工作很好。 – James 2012-08-12 01:05:43

相關問題