0%

dom4j获取xml所有节点

public class Dom4jTest {

    public static void main(String[] args) throws DocumentException {
        String svgURI = System.getProperty("user.dir") + "/test.svg";
        SAXReader reader = new SAXReader();
        File file = new File(svgURI);
        Document document = reader.read(file);
        Element root = document.getRootElement();
        List<Element> childElements = root.elements();

        for (Element child : childElements) {
            if ("g".equals(child.getQName().getName())) {
                List<Element> gElements = child.elements();
                for (Element gEle : gElements) {
                    if ("g".equals(gEle.getQName().getName())) {
                        List<Element> elements = gEle.elements();
                        for (Element e : elements) {
                            if ("dfg:desc".equals(e.getQName().getName())) {
                                System.out.println(e.getText());
                            }
                        }
                    }
                }
            }
        }
    }
}