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. ideau 2018.1.2安装和使用

    此博文的各安装软件.方法技巧仅供研究使用,请勿用于商业活动.下载.操作后请于24小时内删除.对于使用过程中出现的一切问题.责任.纠纷,概不负责. 1.下载ideau-2018.1.2,点击下载,提取码 ...

  2. 解决linux mysql命令 bash: mysql: command not found 的方法

    错误: root@DB-02 ~]# mysql -u root-bash: mysql: command not found 原因:这是由于系统默认会查找/usr/bin下的命令,如果这个命令不在这 ...

  3. 何凯文每日一句打卡||DAY4

  4. jQuery下ajax事件的简单分析

    昨天写了一篇关于监视页面动态生成元素问题的文章,引起了一些小小的争议,不过我从中学到了很多.文章在这,<jQuery下实现等待指定元素加载完毕>当然 动态生成的节点元素 分很多种情况,这里 ...

  5. python中的__new__、__init__和__del__

    __new__.__init__.__del__三个方法用于实例的创建和销毁,在使用python的类中,我们最常用的是__init__方法,通常称为构造方法,__new__方法几乎不会使用,这篇文章是 ...

  6. asp.net mvc 根据路由生成正确的url

    假设存在这样一段路由配置: routes.MapRoute( name: "ProductList1_01", url: "pl/{bigSortId}_{smallSo ...

  7. springboot整合rabbirmq(3.7.9)中使用mandatory参数获取匹配失败的消息以及存入rabbitmq备份交换器中!

    先说下这个参数的作用: /** * Mandatory为true时,消息通过交换器无法匹配到队列会返回给生产者 * 为false时,匹配不到会直接被丢弃 */在一些特定场景下还是有用处的!接下来说一下 ...

  8. Git 使用规范流程【转】

    转自:http://www.ruanyifeng.com/blog/2015/08/git-use-process.html 作者: 阮一峰 日期: 2015年8月 5日 团队开发中,遵循一个合理.清 ...

  9. C# XML 文档注释

    原文链接:http://www.shinater.com/DocsBuilder/help.html <summary>description</summary> 描述类型或类 ...

  10. intellij 出现“Usage of API documented as @since 1.8+”的解决办法

    intellij 出现“Usage of API documented as @since 1.8+”的解决办法 Usage of API documented as @since 1.8+ This ...