Java 操纵XML之读取XML文件

一、JAVA DOM PARSER

DOM interfaces
  The DOM defines several Java interfaces. Here are the most common interfaces:
    Node - The base datatype of the DOM.
    Element - The vast majority of the objects you'll deal with are Elements.
    Attr Represents an attribute of an element.
    Text The actual content of an Element or Attr.
    Document Represents the entire XML document. A Document object is often referred to as a DOM tree.
Common DOM methods
  When you are working with the DOM, there are several methods you'll use often:
    Document.getDocumentElement() - Returns the root element of the document.
    Node.getFirstChild() - Returns the first child of a given Node.
    Node.getLastChild() - Returns the last child of a given Node.
    Node.getNextSibling() - These methods return the next sibling of a given Node.
    Node.getPreviousSibling() - These methods return the previous sibling of a given Node.
    Node.getAttribute(attrName) - For a given Node, returns the attribute with the requested name.

二、源代码:ReadXmlFile.java

 package cn.com.zfc.lesson26.xml;

 import java.io.File;

 import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList; /**
* 使用JAVA DOM PARSER:读取 XML 文件
*
* @author zfc
* @date 2017年12月7日 下午7:17:24
*/
public class ReadXmlFile {
public static void main(String[] args) { try {
String filePath = "I:\\code_workspace\\JavaSE_workspace\\JavaSE\\src\\cn\\com\\zfc\\lesson26\\xml\\ReadXmlFile.xml";
// 1、创建 File 对象,映射 XML 文件
File file = new File(filePath);
// 2、创建 DocumentBuilderFactory 对象,用来创建 DocumentBuilder 对象
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
// 3、创建 DocumentBuilder 对象,用来将 XML 文件 转化为 Document 对象
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
// 4、创建 Document 对象,解析 XML 文件
Document document = documentBuilder.parse(file);
// 5、调用方法
document.getDocumentElement().normalize();
// 6、获取根元素
String studentsName = document.getDocumentElement().getNodeName();
System.out.println("根元素是:" + studentsName);
// 7、获取所有的 student 结点
NodeList students = document.getElementsByTagName("student");
// 8、遍历所有的 student 结点
for (int i = 0; i < students.getLength(); i++) {
// 获取当前的结点 student
Node node = students.item(i);
System.out.println("当前元素是第" + (i + 1) + "个元素:" + node.getNodeName());
// 判断当前的结点类型
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
// 获取当前结点的属性 id
System.out.println("id:" + element.getAttribute("id"));
// 获取当前结点 student 的子结点 name 结点
Element name = (Element) element.getElementsByTagName("name").item(0);
// 输出 name 结点的文本值
System.out.println("name:" + name.getTextContent());
// 获取当前结点 student 的子结点 sex 结点
Element sex = (Element) element.getElementsByTagName("sex").item(0);
// 输出 sex 结点的文本值
System.out.println("sex:" + sex.getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<students>
<student id="student1">
<name>Tom</name>
<sex>Female</sex>
</student>
<student id="student2">
<name>Lucy</name>
<sex>Male</sex>
</student>
</students>

三、运行效果:

Java 操纵XML之读取XML文件的更多相关文章

  1. C#基础精华06(Linq To XML,读取xml文件,写入xml)

    1.XML概述: 可扩展标记语言XML(eXtensible Markup Language)是一种简单灵活的文本格式的可扩展标记语言,侧重于存储数据. 2.XML特点 xml 标记语言 html x ...

  2. Java使用ResourceBundle类读取properties文件中文乱码的解决方案

    Java使用java.util.ResourceBundle类的方式来读取properties文件时不支持中文,要想支持中文必须将文件设置为ISO-8859-1编码格式,这对于开发工具默认为UTF-8 ...

  3. 利用反射与dom4j读取javabean生成对应XML和读取XML得到对应的javabean对象集合

    转自:http://blog.csdn.net/zhao19861029/article/details/8473245 首先实现生成对应的JAVAbean的XML文件方法 /** * DMO4J写入 ...

  4. marshaller unmarshaller解析xml和读取xml

    JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实例文档反向 ...

  5. java中利用RandomAccessFile读取超大文件

    超大文件我们使用普通的文件读取方式都很慢很卡,在java中为我提供了RandomAccessFile函数,可以快速的读取超大文件并且不会感觉到卡哦,下面看我的一个演示实例. 服务器的日志文件往往达到4 ...

  6. Java使用IO流读取TXT文件

    通过BufferedReader读取TXT文件window系统默认的编码是GBK,而IDE的编码多数为UTF-8,如果没有规定new InputStreamReader(new FileInputSt ...

  7. java 加载并读取Properties 文件

    1 .系统自带的application.properties  (以下代码仅供参考,不能粘贴复制) 假设application.properties文件有下面两个值: come.test.name = ...

  8. java 实现poi方式读取word文件内容

    1.下载poi的jar包 下载地址:https://www.apache.org/dyn/closer.lua/poi/release/bin/poi-bin-3.17-20170915.tar.gz ...

  9. java使用io流读取windows文件乱码问题

    出现原因: 在IDEA中,使用 FileReader 读取项目中的文本文件.由于IDEA的设置,都是默认的 UTF-8 编码,所以没有任何 问题. 但是,当读取Windows系统中创建的文本文件时,由 ...

随机推荐

  1. 风控模型-美国FICO标准

    python信用评分卡(附代码,博主录制) https://study.163.com/course/introduction.htm?courseId=1005214003&utm_camp ...

  2. sql查询结果存入DataTable,然后从DataTable取数据

    public static DataTable SqlConnectionInformation() { string connstr = ConfigurationManager.Connectio ...

  3. CXF wsdl2java 生成java代码供客户端使用

    CXF wsdl2java 生成java代码供客户端使用 环境配置:1.下载apache-cxf-2.6.2在环境变量中配置CXF_HOME 值为E:\gavin\cxf\apache-cxf-3.0 ...

  4. linux服务器安装mysql并配置外网访问

    linux服务器安装mysql并配置外网访问 更新系统,如果不运行该命令,直接安装mysql,会出现"有几个软件包无法下载 sudo apt-get update 安装mysql sudo ...

  5. SQL记录-PLSQL字符串

    PL/SQL字符串 PL/SQL字符串实际上是一个可选的尺寸规格字符序列.字符可以是数字,字母,空白,特殊字符或全部的组合. PL/SQL提供了三种类型的字符串: 固定长度字符串:在这样的字符串,程序 ...

  6. bzoj千题计划199:bzoj1055: [HAOI2008]玩具取名

    http://www.lydsy.com/JudgeOnline/problem.php?id=1055 区间DP dp[i][j][k] 表示区间[i,j]能否合成k #include<cst ...

  7. linux课程总结

    linux课程总结 --20125111 李冰清 转眼间,为期十六周的linux课程已进入尾声,回想起这十六周的课程,不断浮现在脑海里的是娄老师的笑容以及这十六周以来的点点滴滴. 第一次听到娄老师说将 ...

  8. .NET面试题系列(七)IIS

    应用程序池的集成模式和经典模式的区别 应用程序池模式会影响服务器处理托管代码请求的方式. 如果托管应用程序在采用集成模式的应用程序池中运行,服务器将使用 IIS 和 ASP.NET 的集成请求处理管道 ...

  9. 控制台console对象常用的一些方法

    console.log():调试中最常用的方法,用于在控制台窗口显示信息. console.log(123); console.warn():输出信息时,在最前面加一个黄色三角,表示警告 consol ...

  10. javascript类式继承函数最优版

    直接上代码: klass函数 var klass = function (Parent, props) { var Child, F, i; //1.新构造函数 Child = function () ...