2010-12-22 68 views
2

所有,高效的XML解析:類型

我有一個大的XML文件的目錄具有以下設置:

<io:InfoObjects xmlns:crole="http://enterprise.businessobjects.com/3.0/customrole" 
xmlns:fo="http://enterprise.businessobjects.com/3.0/folder" 
xmlns:io="http://enterprise.businessobjects.com/3.0/infoobject" 
xmlns:md.dc="http://enterprise.businessobjects.com/3.0/metadata.dataconnection" 
xmlns:un="http://enterprise.businessobjects.com/3.0/universe" 
xmlns:wi="http://enterprise.businessobjects.com/3.0/webi" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://enterprise.businessobjects.com/3.0/customrole BusinessObjects_CustomRole.xsd 
http://enterprise.businessobjects.com/3.0/folder BusinessObjects_Folder.xsd 
http://enterprise.businessobjects.com/3.0/infoobject BusinessObjects_InfoObject.xsd 
http://enterprise.businessobjects.com/3.0/metadata.dataconnection BusinessObjects_MetaData_DataConnection.xsd 
http://enterprise.businessobjects.com/3.0/universe BusinessObjects_Universe.xsd 
http://enterprise.businessobjects.com/3.0/webi BusinessObjects_Webi.xsd" version="1200" illegalCharsEncoded="true"> 
<io:InfoObject xsi:type="wi:Webi"> 
    <io:ID>1</io:ID> 
    <io:Name>MyName</io:Name> 
    <io:Description>NoDesc</io:Description> 
</io:InfoObject> 
</io:InfoObjects> 

原題: (Java 1.5中)我目前使用並通過每個節點/ elemnt進行解析,直到找到我的特定類型的wi:Webi。這看起來很糟糕,並且覺得我錯過了一個Java函數,它允許我簡單地提取所有類型爲「wi:Webi」的元素/節點。有一個更簡單的解決方案嗎?

更新: 我開始使用XPath,但是在創建表達式時遇到問題。到目前爲止,我想:

xpath.compile("//*[@xsi:type='wi:Webi']"); 

根據其他計算器的帖子誰了,我需要在我的表情命名空間來定義類似的問題。我希望* [@ xsi:type]搜索可以顯示任何類型爲wi的孩子:Webi會像在nodelist中一樣返回它。

+1

如果你使用XPath 1.0,那麼你需要註冊`xsi`前綴 - 綁定屬性名稱test的@ http:// www.w3.org/2001/XMLSchema-instance`命名空間URI @xsi: type`。 SO中有很多關於在java中註冊命名空間的答案。 – 2010-12-22 13:19:31

+0

你說得對。我通過創建一個實現了NamespaceContext的類來註冊名稱空間,然後在我的xpath.compile(「// * [@ xsi:type ='wi:Webi']」),這似乎給了我正確的答案。 – XanderLynn 2010-12-22 17:07:09

回答

1

「運行快速和輕量化」的效率,然後是「開發人員編寫更少的代碼行」效率。你最關心哪個?

如果速度很快,請查看SAX處理過程,因爲如果您的要求能夠滿足要求,它不會被誇大成多高效。這是從DOM到SAX的一種心理調整,但我推薦它。

否則,如果您需要堅持使用DOM,請查看XPath以輕鬆提取所有匹配的元素。