2010-12-22 72 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以轻松提取所有匹配的元素。