「java读取节点」java根节点

博主:adminadmin 2022-11-30 13:58:05 44

本篇文章给大家谈谈java读取节点,以及java根节点对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java如何从一个xml文件读取根节点、子节点属性。

思路如下:

xml文件未知不明白什么意思?我索性就理解为一个目录下有很多xml文件,每个xml文件获取其中的bean节点属性。

1 有xml文件路径,获取该路径下的所有文件,用后缀“.xml”或“.XML”过滤得到xml文件。

2 javax.xml.parsers.DocumentBuilder builder = factory.newDocumentBuilder();

org.w3c.dom.Document doc = builder.parse(is); 创建文档对象。

3doc.getChildNodes()获取文档中所有的节点,循环遍历所得节点node,

通过node.getAttributes()获取节点所有属性,获取各个属性name和值即可,输出想要得到的数据。

或者通过doc.getElementsByTagName("bean");直接指定bean节点。然后用同样的方法获取属性名和值,输出。

以上是根据jdk的W3C库解析的。想方便可以通过dom4j、jdom进行文件操作。思路变化不大。

java如何读取xml节点元素值?

java读取xml节点元素,主要使用java提供的解析xml的工具类SAXParserFactory,如下代码:

package xml.xmlreader;

import java.io.File;

import java.net.URL;

import java.util.Properties;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

public class CFGParser {//解析xml文件的工具类

    private Properties props;

    public Properties getProps() {

        return props;

    }

    public void setProps(Properties props) {

        this.props = props;

    }

    public void parse(String filename) throws Exception

    {

        CFGHandler handler = new CFGHandler();

        SAXParserFactory factory = SAXParserFactory.newInstance();

        factory.setNamespaceAware(false);

        factory.setValidating(false);

        SAXParser parser = factory.newSAXParser();

        URL confURL = super.getClass().getClassLoader().getResource(filename);

        if (confURL == null) {

            System.out.println("Can't find configration file.");

            return;

        }

        try

        {

            parser.parse(confURL.toString(), handler);

            this.props = handler.getProps();

        }

        finally {

            factory = null;

            parser = null;

            handler = null;

        }

    }

    public void parseFile(String filename)

    throws Exception

    {

        CFGHandler handler = new CFGHandler();

        SAXParserFactory factory = SAXParserFactory.newInstance();

        factory.setNamespaceAware(false);

        factory.setValidating(false);

        SAXParser parser = factory.newSAXParser();

        File f = new File(filename);

        if ((f == null) || (!f.exists()))

            return;

        try

        {

            parser.parse(f, handler);

            this.props = handler.getProps();

        }

        finally {

            factory = null;

            parser = null;

            handler = null;

        }

    }

}

package xml.xmlreader;

import java.util.Properties;

import org.xml.sax.Attributes;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

public class CFGHandler extends DefaultHandler

{

  private Properties props;

  private String currentSet;

  private String currentName;

  private StringBuffer currentValue = new StringBuffer();

  public CFGHandler()

  {

    this.props = new Properties();

  }

  public Properties getProps() {

    return this.props;

  }

  public void startElement(String uri, String localName, String qName, Attributes attributes)

    throws SAXException

  {

    this.currentValue.delete(0, this.currentValue.length());

    this.currentName = qName;

  }

  public void characters(char[] ch, int start, int length) throws SAXException

  {

    this.currentValue.append(ch, start, length);

  }

  public void endElement(String uri, String localName, String qName)

    throws SAXException

  {

    this.props.put(qName.toLowerCase(), this.currentValue.toString().trim());

  }

}

xml文件

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

xml-body

        refresh_userlist desc="用户列表刷新间隔时间(秒)"6/refresh_userlist

        refresh_message desc="短消息刷新间隔时间(秒)"10/refresh_message

        morningbegin desc="上午上班时间"23:00/morningbegin

        morningend desc="上午下班时间"12:00/morningend

        afternoonbegin desc="下午上班时间"18:00/afternoonbegin

/xml-body

jsp获取各个节点的值:

%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%

html

    jsp:useBean id="cfgp" scope="page" class="xml.xmlreader.CFGParser"/jsp:useBean

    body

        %

   cfgp.parse("kaoqin.xml");

   Properties pro = cfgp.getProps();

   String stTime = pro.getProperty("morningbegin");

   String edTime = pro.getProperty("morningend");

    String afternoonbegin = pro.getProperty("afternoonbegin");

   

   out.println(stTime+"\n"+edTime+"\n"+afternoonbegin);

   System.out.println(stTime+"\n"+edTime+"\n"+afternoonbegin);

    %

    /body

/html

用Java读取文件节点信息,哪位有空给个思路啊

这是我刚刚学java的时候写的一个考试系统中从配置文件中读取考试题并将试题插入数据库的代码:

public static void main(String[] args) {

FileInputStream fis=null;

InputStreamReader reader=null;

BufferedReader br=null;

Connection conn=null;

PreparedStatement pstmt=null;

String sql="insert into question(answers,score,level,title,answerA,answerB,answerC,answerD) values(?,?,?,?,?,?,?,?)";

try {

conn=ConnectionUtil.getConnection();

pstmt=conn.prepareStatement(sql);

fis = new FileInputStream("corejava.txt");

reader=new InputStreamReader(fis,"gbk");

br=new BufferedReader(reader);

String str=null;

while((str=br.readLine())!=null){

str=str.trim();

if(str.equals("")||str.endsWith("#")){

continue;

}

String[] data=str.split("[@,][a-z]+=");

pstmt.setString(1, data[1]);

pstmt.setInt(2, Integer.parseInt(data[2]));

pstmt.setInt(3, Integer.parseInt(data[3]));

pstmt.setString(4, br.readLine());//title

pstmt.setString(5, br.readLine());

pstmt.setString(6, br.readLine());

pstmt.setString(7, br.readLine());

pstmt.setString(8, br.readLine());

System.out.println(sql);

pstmt.executeUpdate();

}

} catch (Exception e) {

e.printStackTrace();

}finally{

try {

br.close();

reader.close();

fis.close();

pstmt.close();

conn.close();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

关于java读取节点和java根节点的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-30,除非注明,否则均为首码项目网原创文章,转载请注明出处。