Java读取xml文件里属性

需要解析的xml文件

复制代码
<Data>
       <ElemName caption="lalalala">
           <Color>
            <Value caption="0"/>
            <Value caption="0"/>
            <Value caption="0"/>
        </Color>
        <Level caption="888">
            <MinValue caption="0"/>
            <MaxValue caption="360"/>
            <Interval caption="60"/>
        </Level>
    </ElemName>
<Data>
复制代码

 

代码

复制代码
import java.io.File;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
/**
 * 读取xml文件
 * 
 * @author admin
 *
 */
public class ReadXml {
    /**
     * 获取等值线间隔
     * 
     * @return
     * @throws Exception
     */
    public static double[] getDataInterval(String modelType, String element, int level) throws Exception {
        SAXReader reader = new SAXReader();
        Properties pro = PropertiesUtil.getInstance().getProperties("env.properties");
        String READ_DATAINTERVAL_PATH = pro.getProperty("READ_DATAINTERVAL_PATH");//
        Document document = reader.read(new File(READ_DATAINTERVAL_PATH + modelType + ".xml"));

        // 获取文档根节点
        Element root = document.getRootElement();
        // 输出根标签的名字
        System.out.println(root.getName());

        // 获取根节点下面的所有子节点(不包过子节点的子节点)
        List<Element> list = root.elements();
        // 遍历List的方法
        for (Element e : list) {
            if (e.attributeValue("caption").equals(element)) {
                List<Element> ListElement = e.elements("Level");// 获取符合条件的e节点的所有子节点
                for (Element f : ListElement) {
                    if (f.attributeValue("caption").equals(Integer.toString(level))) {
                        double min = Double.parseDouble(f.element("MinValue").attributeValue("caption"));// 获取符合条件的f节点下的MinValue节点的caption属性
                        double max = Double.parseDouble(f.element("MaxValue").attributeValue("caption"));
                        double interval = Double.parseDouble(f.element("Interval").attributeValue("caption"));
                        double[] dataInterval = new double[new Double((max - min) / interval).intValue()];
                        double i = min;
                        for (int j = 0; j < dataInterval.length; j++) {

                            if (i <= max) {
                                dataInterval[j] = i;
                                i = i + interval;
                            }

                        }
                        System.out.println(dataInterval.toString());
                        return dataInterval;
                    }
                }
            }
        }
        System.out.println("获取等值线间隔失败");
        return null;

    }

}
复制代码

 

posted on   jianglusheng  阅读(2426)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示