public static void main(String[] args) throws DocumentException {
        SAXReader saxReader = new SAXReader();
        Document read = saxReader.read(new File("E://work//spring-ioc//src//main//resources//stu.xml"));
        // 获取根节点
        Element rootElement = read.getRootElement();
        getNodes(rootElement);
 
        }
 
static public void getNodes(Element rootElement) {
        System.out.println("当前节点名称:" + rootElement.getName());
        // 获取属性ID
        List<Attribute> attributes = rootElement.attributes();
        for (Attribute attribute : attributes) {
        System.out.println("属性:" + attribute.getName() + "---" + attribute.getText());
        }
        if (!rootElement.getTextTrim().equals("")) {
        System.out.println(rootElement.getName() + "--" + rootElement.getText());
        }
        // 使用迭代器遍历
        Iterator<Element> elementIterator = rootElement.elementIterator();
        while (elementIterator.hasNext()) {
        Element next = elementIterator.next();
        getNodes(next);
        }
 
        }

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-28
  • 2021-11-08
  • 2021-12-06
  • 2022-02-27
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-18
  • 2021-05-08
  • 2022-02-28
  • 2022-12-23
  • 2022-01-23
  • 2021-06-07
  • 2022-12-23
相关资源
相似解决方案