是否可以使用apache摘要生成器提取节点名称?

因此,如果xml看起来像

   <furniture>
     <sofa>
       .....
     </sofa>
     <coffeeTable>
       .....
     </coffeeTable>
   </furniture>


是否可以提取节点名称“ sofa”,“ coffeeTable”?

我知道可以使用xpath,但是可以使用摘要器吗?

干杯

最佳答案

(原始答案)

使用简单的Digester为模式"furniture/*"创建一个Rule,该简单的furniture将每个参数都包含在对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);
    }
}


此特定示例将打印所有元素名称,包括根match()元素。我将留给您根据您的需要调整方法。

关于java - 摘要器:提取节点名称,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2165168/

10-14 09:45
查看更多