Digester: Extracting node name

前端 未结 2 1868
闹比i
闹比i 2021-01-23 22:24

Is it possible to extract the node name using apache digester?

So, if the xml looks like

   
     
       .....
              


        
2条回答
  •  逝去的感伤
    2021-01-23 23:09

    This is the kind of matching that ExtendedBaseRules afford.

    Let's say this is the content of furniture.xml:

    
       
          
       
       
          
       
    
    

    Let's say you want to get the element names of the direct children of furniture element. This is what furniture/? matches in the ExtendedBaseRules.

    import java.io.*;
    import java.util.*;
    import org.apache.commons.digester.*;
    
    public class FurnitureDigest {
        public static void main(String[] args) throws Exception {
            File file = new File("furniture.xml");
            Digester digester = new Digester();
            digester.setRules(new ExtendedBaseRules());
            final List furnitures = new ArrayList();
            digester.addRule("furniture/?", new Rule() {
                @Override public void end(String nspace, String name) {
                    furnitures.add(name);
                }
            });
            digester.parse(file);
            System.out.println(furnitures); // prints "[sofa, coffeeTable]"
        }
    }
    

    API links

    • Package org.apache.commons.digester
    • org.apache.commons.digester.Rule
    • org.apache.commons.digester.ExtendedBaseRules

提交回复
热议问题