码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 根据标签名递归读取xml字符串中element


    工具类:

      /**
       * 根据标签名递归读取xml字符串中element
       * 例:
       * String xml =
       * "\n" +
       * "\n" +
       * "\n" +
       * "  \n" +
       * "\n" +
       * "\n" +
       * "";
       * 

    * element(xml) => 获得 req element * element(xml, "tag1") => 获得 tag1 element * element(xml, "tag2", "tag4") => 获得 tag4 element * * @param xml * @param nodes * @return */ public static Element element(String xml, String... nodes) { try { Document document = DocumentHelper.parseText(xml); Element element = document.getRootElement(); for (String node : nodes) { element = element.element(node); } return element; } catch (DocumentException e) { e.printStackTrace(); } return null; }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    测试:

    public class Main {
        public static void main(String[] args) {
            String xmlString = "Harry Potter";
    
            // 获取 books 元素
            Element booksElement = element(xmlString);
            System.out.println("Books: " + booksElement.asXML());
    
            // 获取 category 元素
            Element categoryElement = element(xmlString, "category");
            System.out.println("Category: " + categoryElement.asXML());
    
            // 获取 book 元素
            Element bookElement = element(xmlString, "category", "book");
            System.out.println("Book: " + bookElement.asXML());
        }
    
        // 之前的 element 函数
        public static Element element(String xml, String... nodes) {
            // ...
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    输出:

    Books: <books><category name="Fiction"><book id="1">Harry Potter</book></category></books>
    Category: <category name="Fiction"><book id="1">Harry Potter</book></category>
    Book: <book id="1">Harry Potter</book>
    
    • 1
    • 2
    • 3
  • 相关阅读:
    宏定义天坑记录
    801a qcn文件IMEI修改
    C++ Date 类的编写
    spring cloud 快速上手系列 -> 03-消息队列 Stream -> 032-使用spring cloud bus实现配置中心热刷新-Server
    CoDeSys系列-4、基于Ubuntu的codesys运行时扩展包搭建Profinet主从环境
    databinding 一篇文章就够了
    __cpu_online_mask的设置
    Pytorch DDP原理及配置[最大限度提高GPU利用率]
    springboot/java/php/node/python微信点餐系统【计算机毕设】
    缓存设计的创新之旅:架构的灵魂之一
  • 原文地址:https://blog.csdn.net/weixin_46399670/article/details/139093862
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号