java读取xml文件

java读取xml文件

文件内容

第一:写好文件为one.xml,内容如下:

<a>
    <b>
        <c1>
            <First id="1" name="one"/>
            <First id="2" name="two"/>
            <First id="31" name="three"/>
        </c1>
        <c2>
            <!--与上面的First类似-->
        </c2>
    </b>
</a>

第二:写好一个类First.java,里面有两个属性id和name,然后get和set方法。

第三:再写一个类为Test.java。写main方法,在main方法里写入

SAXReader saXReader = new SAXReader();
URL realUrl = RateContext.class.getClassLoader().getResource("\\one.xml");
Document doc;
try {
    doc = saXReader.read(realUrl);
    List<First> list = new ArrayList<>();
    String path="/a/b/c1";
    List<First> list1 = rc.addEle(path, list, First.class, doc);
    for(int i=0; i<list1.size(); i++){
        System.out.println(list1.get(i).getId() + " , " + list1.get(i).getName());
    }
} catch (DocumentException e) {
    e.printStackTrace();
}

第四:另外在这个Test.java中写两个方法

public static List addEle(String xPath, List list, Class clazz, Document doc) {
    List<Element> Info = XmlHelper.parseXmlEleByXpath(doc, xPath);
    Field[] fields = clazz.getDeclaredFields();
    for (Element e : Info) {
        try {
            Object object = clazz.newInstance();
            for (Field field : fields) {
                Method method = clazz.getMethod("set" + toUpperCase(field.getName()), field.getType());
                if(field.getType().getName().equals("int")){
                    method.invoke(object, new Object[] {Integer.parseInt(e.attributeValue(field.getName()) )});
                }else{
                    method.invoke(object, new Object[] {e.attributeValue(field.getName()) });
                }
            }
            list.add(object);
        } catch (SecurityException e1) {
            logger.info(e1.getMessage(), e1);
        } catch (NoSuchMethodException e2) {
            logger.info(e2.getMessage(), e2);
        } catch (IllegalArgumentException e3) {
            logger.info(e3.getMessage(), e3);
        } catch (IllegalAccessException e4) {
            logger.info(e4.getMessage(), e4);
        } catch (InvocationTargetException e5) {
            logger.info(e5.getMessage(), e5);
        } catch (InstantiationException e6) {
            logger.info(e6.getMessage(), e6);
        }
    }
    return list;
}

public static String toUpperCase(String word) {
    return word.substring(0, 1).toUpperCase() + word.substring(1);
}

打印结果

最后就可以把工程跑起来了,然后打印出来,

  1. one
  2. two
  3. three

Read more

超快速,使用ChatGPT编写回归和分类算法

超快速,使用ChatGPT编写回归和分类算法

本文将使用一些 ChatGPT 提示,这些提示对于数据科学家在工作时非常重要。 微信搜索关注《Python学研大本营》,加入读者群,分享更多精彩 以下是一些示例ChatGPT 提示的列表以及数据科学家的响应。 ChatGPT 提示 为决策树回归算法生成 python 代码。 下面是使用scikit-learn在 Python 中进行决策树回归的示例代码: import numpy as np import matplotlib.pyplot as plt from sklearn.tree import DecisionTreeRegressor # Generate random data rng = np.random.default_rng() x = 5 * rng.random(100) y = np.sin(x) + 0.

By Ne0inhk
力扣每日一题:993.二叉树的堂兄弟节点 深度优先算法

力扣每日一题:993.二叉树的堂兄弟节点 深度优先算法

993.二叉树的堂兄弟节点 难度:简单 题目: 在二叉树中,根节点位于深度 0 处,每个深度为 k 的节点的子节点位于深度 k+1 处。 如果二叉树的两个节点深度相同,但 父节点不同 ,则它们是一对堂兄弟节点。 我们给出了具有唯一值的二叉树的根节点 root ,以及树中两个不同节点的值 x 和 y 。 只有与值 x 和 y 对应的节点是堂兄弟节点时,才返回 true 。否则,返回 false。 示例: 示例 1: 输入:root = [1,2,3,4], x = 4, y = 3 输出:false

By Ne0inhk
1239.串联字符串的最大长度 关于字符串的回溯算法!

1239.串联字符串的最大长度 关于字符串的回溯算法!

题目: 给定一个字符串数组 arr,字符串 s 是将 arr 某一子序列字符串连接所得的字符串, 如果 s 中的每一个字符都只出现过一次,那么它就是一个可行解。 请返回所有可行解 s 中最长长度。 提示: 1 <= arr.length <= 16 1 <= arr[i].length <= 26 arr[i] 中只含有小写英文字母 示例: 示例 1: 输入:arr = ["un","iq","ue"] 输出:4 解释:所有可能的串联组合是

By Ne0inhk