是否可以使用apache消化器提取节点名称?
所以,如果 xml 看起来像
<furniture>
<sofa>
.....
</sofa>
<coffeeTable>
.....
</coffeeTable>
</furniture>
是否可以提取节点名称“sofa”、“coffeeTable”?
我知道可以使用xpath,但是可以使用digester吗?
干杯
最佳答案
(原答案)
使用简单的规则
为模式“furniture/*”
创建一个Digester
,该规则将第二个参数传递给对begin方法的每次调用,并将其粘贴到您选择的集合中(一个用于获取所有这些参数的列表,一个用于仅获取所有唯一名称的集合)。
(编辑)
从头开始,这有点复杂。
这有效:
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);
}
}
此特定示例将打印所有元素名称,包括根 furniture
元素。我将让您根据需要调整 match()
方法。
关于java - 消化器 : Extracting node name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2165168/