沼气池:提取节点名称 [英] Digester: Extracting node name
问题描述
是否可以使用Apache沼气池提取节点名称?
Is it possible to extract the node name using apache digester?
所以,如果XML看起来像
So, if the xml looks like
<furniture>
<sofa>
.....
</sofa>
<coffeeTable>
.....
</coffeeTable>
</furniture>
是它可以提取节点名称沙发,coffeeTable?
is it possible to extract the node name "sofa", "coffeeTable"?
我知道这是可能使用XPath,但使用沼气池这可能吗?
I know it is possible using xpath but is it possible using digester?
干杯
推荐答案
(原来的答案)
创建一个沼气池
的模式家具/ *
用一个简单的规则
,是以第二个参数每次调用开始方法,并坚持一个您选择的集合(名单让所有的人,一组只得到所有唯一的名称)。
Create a Digester
for pattern "furniture/*"
with a simple Rule
that takes the second parameter to each call to the begin method and sticks it in a collection of your choice (a list to get all of them, a set to get only all unique names).
(编辑)
从头开始,它更复杂一些。
Scratch that, it's a bit more complicated.
本作品:
public class App
{
final static Rule printRule = new Rule() {
public void begin(String namespace, String name,
Attributes attributes) throws Exception {
System.out.println(name);
}
};
public static void main( String[] args ) throws IOException, SAXException
{
InputStream instr = App.class.getResourceAsStream("/sample.xml");
Digester dig = new Digester();
dig.setRules(new RulesBase(){
public List<Rule> match(String namespaceURI, String pattern) {
return Arrays.asList(printRule);
}
});
dig.parse(instr);
}
}
这个特殊的样本将打印所有元素名称包括根家具
元素。我要把它留给你的匹配()
方法调整您的需求。
This particular sample will print all element names including the root furniture
element. I'll leave it to you to adjust the match()
method to your needs.
这篇关于沼气池:提取节点名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!