zoukankan      html  css  js  c++  java
  • 从XML文件和properties文件提取数据

    XML文档格式内容如下

    <?xml version="1.0" encoding="UTF-8"?>

    <root>
        <field type="1" store="yes">title1</field>
        <field type="2" store="no">title2</field>
        <field type="3" store="yes">title3</field>
    </root>
    JAVA代码如下
    import java.io.File;
    import java.io.IOException;
     
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
     
    import org.w3c.dom.Document;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;
     
    public class MyXml {
        public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
            //读取XML文件
            File f = new File("E:\workspace\cn.harmel.lucene\src\1.xml");
            //获取DocumentBuilderFactory
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            //通过DocumentBuilder工厂产生一个DocumentBuilder
            DocumentBuilder builder = factory.newDocumentBuilder();
            //利用DocumentBuilder产生Document
            Document doc = builder.parse(f);
            //获取指定的标签的集合
            NodeList nl = doc.getElementsByTagName("field");
            for (int i = 0; i < nl.getLength(); i++) {
               String fieldName=nl.item(i).getFirstChild().getNodeValue();//获取标签值
               String fieldType=nl.item(i).getAttributes().getNamedItem("type").getNodeValue();//获取标签属性值
               String fieldStore=nl.item(i).getAttributes().getNamedItem("store").getNodeValue();//获取标签属性值
               System.out.println(fieldName+"------"+fieldType+"------"+fieldStore);
            }       
        }
    }
     
    public class ECPsUtils {

    public static String resdProp(String key){
    String vlaue = null;
    // InputStream in = ECPsUtils.class.getClassLoader().getResourceAsStream("file.properties");
    //// //读取配置文件
    //// Properties prop = new Properties();
    //// try {
    //// prop.load(in);
    //// vlaue = prop.getProperty(key);
    //// } catch (IOException e) {
    //// e.printStackTrace();
    //// }
    ResourceBundle bundle = ResourceBundle.getBundle("file");
    vlaue = bundle.getString(key);
    return vlaue;
    }
  • 相关阅读:
    使用apache的ab命令进行压测
    mysql插入数据时,去掉重复的数据;
    CI框架的事务开启、提交和回滚
    电脑如何自动玩popstar
    Outsider(HNOI2019)
    洛谷P4689 [Ynoi2016]这是我自己的发明(莫队,树的dfn序,map,容斥原理)
    Ubuntu下编写终端界面交互式C++小程序的一些Trick(小技巧,gnome-terminal)
    贪心相关/模拟网络流、费用流细节梳理/模板(贪心,模拟费用流,栈)
    洛谷P3602 Koishi Loves Segments(贪心,multiset)
    洛谷CF809C Find a car(数位DP)
  • 原文地址:https://www.cnblogs.com/ZTYCandy/p/10685632.html
Copyright © 2011-2022 走看看