当前位置:网站首页>Dom4j+xpath parsing XML files

Dom4j+xpath parsing XML files

2022-06-22 08:03:00 Chasing dream Zichen

dom4j+xpath analysis xml file

dom4j+xpath analysis xml file document for java SAX
1)xpath Similar to select sentence ;
2)Document With nodes (Node) form : Elements . node 、 attribute 、 Text etc.
3) Node( node ) Element( Elements ) difference getText();
3)selectNodes() Get all child nodes under the node ;
4)selectSingleNode() Get single node information ;
5) xpath grammar :/( Positioning path )、@( get attribute )

public static void main(String[] args) throws DocumentException {
    
        // obtain io flow 
        InputStream is = ConfigText.class.getResourceAsStream("config.xml");
        // Reader 
        SAXReader sr = new SAXReader();
        // Read configuration file , obtain document object 
        Document doc = sr.read(is);
        // Use xpath analysis document object 
        //config node 
        List<Node> config = doc.selectNodes("config");
        // Traverse config node 
        for (Node c:config
             ) {
    
            Map<String,ActionMod> configmap = new HashMap<>();
            //action node 
            List<Node> action = doc.selectNodes("config/action");
            for (Node a:action
                 ) {
    
                Map<String,ForwardMod> actionmap = new HashMap<>();
                Element ea =(Element) a;
                //forward node 
                List<Node> forward = doc.selectNodes("config/action/forward");
                for (Node f:forward
                     ) {
    
                    Element el =(Element) f;
                    actionmap.put(el.attributeValue("name"),new ForwardMod(el.attributeValue("path"),el.attributeValue("redirect")));

                }
                configmap.put(ea.attributeValue("path"),new ActionMod(actionmap,ea.attributeValue("type")));

            }
            System.out.println(configmap);
        }

    }
原网站

版权声明
本文为[Chasing dream Zichen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220800135306.html