2011-08-24 85 views
3

有人能夠幫助我如何實現這一點,或者至少可以使用此算法。將樹結構解析爲關係式數據存儲

我想要做的是將層次結構/樹結構文件解析到關係存儲中。我將在下面進一步解釋一個例子。

這是一個示例源文件,只是針對此問題的簡單/非實際示例。

<title text=「title1"> 
    <comment id=「comment1"> 
     <data> this is part of comment one</data> 
     <data> this is some more of comment one</data> 
    </comment> 
    <comment id=「comment2」> 
     <data> this is part of comment two</data> 
     <data> this is some more of comment two</data> 
     <data> this is even some more of comment two</data> 
    </comment> 
</title> 

所以這裏要注意的主要事情是,<comment>數量,以及<data>元素爲每個評論的數量可以是任意的。因此,鑑於上述情況,我會想變成的東西看起來像:

title  | comment  |  data 
------------------------------------------------------------------------ 
title1  comment1   this is some part of comment one 
title1  comment1   this is some more of comment one 
title1  comment2   this is part of comment two  
title1  comment2   this is some more of comment two 
title1  comment2   this is even some more of comment two 

爲了做到這一點,可以說我可以指定以下方式的關係模式,使用XPath表達式,可以是在源文件上評估。

attribute1: title = /title/@title 
attribute2: comment = /title/comment/@id 
attribute3: data = /title/comment/data/text() 

建議的數據結構:

  • ResultSet是一個List<Map<String,String>>(其中:每個圖表示單個行)
  • Schema是一個Map<String,String>(其中:我們映射屬性名 - - >路徑表達式)
  • 源文件,部分DOM Document
+0

你可以使用像這樣的東西:'HashMap >>' –

+0

我不確定你在這裏問什麼 - 這將是一種方式來存儲它。一旦儲存後你想如何使用它? – Tom

+0

問題是,基本上,如何實現解析器,即給定一個任意的源文件和模式映射,如何將其轉換爲關係式存儲,如圖所示。 – Larry

回答

0

我不確定您是否在問如何實現XML解析器本身,或者如何在給定XML的解析樹的情況下如何將其解壓縮爲層次結構。我猜你現在正在查看後者(有很多很好的XML解析器,我懷疑這是瓶頸),所以我會在這裏回答。讓我知道你是否真的對XML解析細節感興趣,並且我可以更新答案。

我相信你想要考慮的方式是在樹上遞歸下降。這個想法如下:你的命名系統由樹中你上面的所有節點連接起來,並由你自己的名字組成。鑑於這種情況,你可以在樹中使用這樣的運行遞歸DFS:

FlattenXML(XMLDocument x) { 
    for each top-level XML node t: 
     RecFlattenTree(t, ""); 
} 

RecFlattenTree(Tree t, String prefix) { 
    if t is a leaf with data d: 
     update the master table by adding (prefix, d) to the list of entries 
    else 
     for each child c of t, whose name is x: 
      RecFlattenTree(c, prefix + "/" + x) 
} 

例如,如果你要在你不得不往上頂的XML文檔跟蹤此,它可能會去是這樣的:

RecFlattenTree(title1, "/title1") 
    RecFlattenTree(comment1, "/title1/comment1") 
     RecFlattenTree(data node 1 , "/title1/comment1") 
      Add /title1/comment1/data, value = "this is some part of comment one" 
     RecFlattenTree(data node 2, "/title1/comment1") 
      Add /title1/comment2/data, value = "this is some more of comment one" 
    RecFlattenTree(comment2, "/title1/comment2") 
     RecFlattenTree(data node 1 , "/title1/comment2") 
      Add /title1/comment2/data, value = "this is part of comment two" 
     RecFlattenTree(data node 2, "/title1/comment2") 
      Add /title1/comment2/data, value = "this is more of comment two" 
     RecFlattenTree(data node 3, "/title1/comment2") 
      Add /title1/comment2/data, value = "this is even more of comment two" 

這最終產生的名單

/title1/comment1/data, value = "this is some part of comment one" 
/title1/comment1/data, value = "this is some more of comment one" 
/title1/comment1/data, value = "this is part of comment two" 
/title1/comment1/data, value = "this is more of comment two" 
/title1/comment1/data, value = "this is even more of comment two" 

這是你想要什麼。

希望這會有所幫助!如果我誤解了你的問題,請告訴我!

+0

謝謝,你一定理解我的問題,雖然我可能忘了提及xml /樹文件也可能包含與該組屬性無關的其他數據。那麼在這種情況下,我們需要遍歷給定的一組屬性,而不是遍歷整個xml文件?! 我也在Java中編寫這個代碼,所以如果任何人都可以在Java中提出一個完美的解決方案。 – Larry

+0

順便說一句,你的問題似乎是對象關係映射(ORM)類別。例如Hibernate具有將xml文件轉換爲SQL數據庫表的功​​能。嘗試搜索hibernate源代碼以查找執行hbm.xml到db轉換的部分。 – othman