說我有以下XML:如何將XML文檔拆分爲特定標籤之間的字符串?
<foo>
<spam taste="great"> stuff</spam> <spam taste="moldy"> stuff</spam>
<bar taste="eww"> stuff </bar> <bar> stuff </bar>
<bacon taste="yum"> stuff </bacon><bacon taste="yum"> stuff </bacon><bacon taste="yum"> stuff </bacon>
</foo>
垃圾郵件,酒吧和燻肉更多的標籤是數據的標籤裏面,我想將XML分成這
<spam taste="great"> stuff</spam> <spam taste="moldy"> stuff</spam>
,<bar taste="eww"> stuff </bar> <bar> stuff </bar>
,<bacon taste="yum"> stuff </bacon><bacon taste="yum"> stuff </bacon><bacon taste="yum"> stuff </bacon>
,
爲了重新排序它進行解析。
像這樣的基本結構,塊以任何順序排列。
<foo>
block of bar tags
block of spam tags
block of bacon tags
</foo>
我知道在運行時的名字,我想一起組每種類型的所有標籤。 –
即使每個垃圾郵件,培根和條形碼標籤內都有許多嵌套標籤,這是否也能正常工作? –
@JamesLu - 如果你知道運行時的名字,並且不關心保持每個組的獨立性(即你只想要_all_ spam元素,_all_ bar元素等等),那麼Daniel的解決方案可能對你更好。如果你想保留每個塊(因此垃圾郵件欄垃圾郵件將導致3個組),那麼我的解決方案會更好。嵌套:我的解決方案將忽略標籤內的任何嵌套+保持相同。不知道這是你在找什麼。 – Michael0x2a