matlab分析xml文件

Today we will learn how to read the XML file in Java. We will also learn how to parse an XML file in java to object using DOM parser.

今天,我们将学习如何使用Java读取XML文件。 我们还将学习如何使用DOM解析器将Java中的XML文件解析为对象。

DOM XML Parser is easiest to understand. It loads the XML object into memory as Document, then you can easily traverse different elements and nodes in the object. The traversing of elements and nodes are not required to be in order.

DOM XML分析器最容易理解。 它将XML对象作为Document加载到内存中,然后您可以轻松地遍历对象中的不同元素和节点。 元素和节点的遍历不需要按顺序进行。

如何用Java读取XML文件 (How to read XML File in Java)

DOM Parser are good for small XML documents but since it loads complete XML file into memory, it’s not good for large XML files. For large XML files, you should use SAX Parser.

DOM Parser对于小型XML文档很有用,但是由于它将完整的XML文件加载到内存中,因此对于大型XML文件不是很好。 对于大型XML文件,应使用SAX Parser 。

In this tutorial, we will read the XML file and parse it to create an object from it.

在本教程中,我们将读取XML文件并进行解析以从中创建对象。

Here is the XML file that will be read in this program.

这是将在此程序中读取的XML文件。

employee.xml

employee.xml

<?xml version="1.0"?>
<Employees><Employee><name>Pankaj</name><age>29</age><role>Java Developer</role><gender>Male</gender></Employee><Employee><name>Lisa</name><age>35</age><role>CSS Developer</role><gender>Female</gender></Employee>
</Employees>

So this XML is the list of employees, to read this XML file I will create a bean object Employee and then we will parse the XML to get the list of employees.

因此,此XML是雇员列表,要读取此XML文件,我将创建一个bean对象Employee,然后我们将解析XML以获取雇员列表 。

Here is the Employee bean object.

这是Employee bean对象。

package com.journaldev.xml;public class Employee {private String name;private String gender;private int age;private String role;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getRole() {return role;}public void setRole(String role) {this.role = role;}@Overridepublic String toString() {return "Employee:: Name=" + this.name + " Age=" + this.age + " Gender=" + this.gender +" Role=" + this.role;}}

Notice that I have overridden toString() method to print useful information about the employee.

注意,我已经重写了toString()方法以打印有关员工的有用信息。

Read this post to know you should always use @Override annotation to override methods.

阅读这篇文章,以了解您应该始终使用@Override注解覆盖方法。

If you are new to annotations, read java annotations tutorial.

如果您不熟悉注释,请阅读Java注释教程 。

Java DOM解析器 (Java DOM Parser)

Here is the java program that uses DOM Parser to read and parse XML file to get the list of Employee object.

这是使用DOM解析器读取和解析XML文件以获得Employee对象列表的Java程序。

package com.journaldev.xml;import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;public class XMLReaderDOM {public static void main(String[] args) {String filePath = "employee.xml";File xmlFile = new File(filePath);DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();DocumentBuilder dBuilder;try {dBuilder = dbFactory.newDocumentBuilder();Document doc = dBuilder.parse(xmlFile);doc.getDocumentElement().normalize();System.out.println("Root element :" + doc.getDocumentElement().getNodeName());NodeList nodeList = doc.getElementsByTagName("Employee");//now XML is loaded as Document in memory, lets convert it to Object ListList<Employee> empList = new ArrayList<Employee>();for (int i = 0; i < nodeList.getLength(); i++) {empList.add(getEmployee(nodeList.item(i)));}//lets print Employee list informationfor (Employee emp : empList) {System.out.println(emp.toString());}} catch (SAXException | ParserConfigurationException | IOException e1) {e1.printStackTrace();}}private static Employee getEmployee(Node node) {//XMLReaderDOM domReader = new XMLReaderDOM();Employee emp = new Employee();if (node.getNodeType() == Node.ELEMENT_NODE) {Element element = (Element) node;emp.setName(getTagValue("name", element));emp.setAge(Integer.parseInt(getTagValue("age", element)));emp.setGender(getTagValue("gender", element));emp.setRole(getTagValue("role", element));}return emp;}private static String getTagValue(String tag, Element element) {NodeList nodeList = element.getElementsByTagName(tag).item(0).getChildNodes();Node node = (Node) nodeList.item(0);return node.getNodeValue();}}

Output of the above program is:

上面程序的输出是:

Root element :Employees
Employee:: Name=Pankaj Age=29 Gender=Male Role=Java Developer
Employee:: Name=Lisa Age=35 Gender=Female Role=CSS Developer

In real life, it’s not a bad idea to validate XML file before parsing it to objects, learn how to validate XML against XSD in java. That’s all about how to read xml file or parse xml file in java.

在现实生活中,在将XML文件解析为对象之前验证XML文件,学习如何在Java中针对XSD验证XML并不是一个坏主意。 这就是如何在Java中读取xml文件或解析xml文件的全部内容。

Reference: Official W3.org Doc

参考: W3.org官方文档

翻译自: https://www.journaldev.com/898/read-xml-file-java-dom-parser

matlab分析xml文件

matlab分析xml文件_如何在Java中读取XML文件(DOM分析器)相关推荐

  1. java 读取doc文件_如何在java中读取Doc或Docx文件?

    我想在 java中读一个word文件 import org.apache.poi.poifs.filesystem.*; import org.apache.poi.hpsf.DocumentSumm ...

  2. java 检测目录下的文件_如何在Java中检查文件是目录还是文件

    java 检测目录下的文件 java.io.File class contains two methods using which we can find out if the file is a d ...

  3. java中iterator_如何在Java中读取CSV文件-Iterator和Decorator的案例研究

    java中iterator 在本文中,我将讨论如何使用Apache Common CSV读取CSV(逗号分隔值)文件. 从这个案例研究中,我们将学习如何在设计模式的上下文中使用Iterator和Dec ...

  4. 如何在Java中读取CSV文件-Iterator和Decorator的案例研究

    在本文中,我将讨论如何使用Apache Common CSV读取CSV(逗号分隔值)文件. 从这个案例研究中,我们将学习如何在设计模式的上下文中使用Iterator和Decorator来提高不同情况下 ...

  5. macos 虚拟镜像文件_如何在macOS中使用虚拟文件测试网络或硬盘速度

    macos 虚拟镜像文件 File transfer speeds can vary greatly from device to device. The same holds true for ne ...

  6. python打开文件报错无效序列_如何在python中读取fasta文件?

    我正在尝试读取FASTA文件,然后查找特定的 motif(string)并打印出序列和次数. A FASTA file只是一系列序列(字符串),以标题行开头,标题或新序列的开头是">& ...

  7. chrome查看网页文件_如何在Chrome中直接将文件和网页下载到Google云端硬盘

    chrome查看网页文件 We've all downloaded files from the web to our computer. However, if you'd rather downl ...

  8. java如何新建一个空的压缩包_如何在Java中创建zip文件

    慕哥6287543 Java 7内置了ZipFileSystem,可用于从zip文件创建,写入和读取文件.Java Doc:ZipFileSystem ProviderMap env = new Ha ...

  9. python 读取日志文件_如何在Python中跟踪日志文件?

    使用SH模块(PIP安装sh):from sh import tail# runs foreverfor line in tail("-f", "/var/log/som ...

最新文章

  1. 《BI那点儿事》三国人物智力分布状态分析
  2. 苹果、联想及华硕均看准美国电脑运输的增长
  3. vue非编译的模块化写法
  4. [HDU 4666]Hyperspace[最远曼哈顿距离][STL]
  5. Dubbo消费者代理的调用
  6. 104种***清除方法
  7. php聊天系统文档,聊天后台管理系统接口文档
  8. 序列化之Java默认序列化技术(ObjectOutputStream与ObjectInputStream)
  9. 阶段3 1.Mybatis_05.使用Mybatis完成CRUD_2 Mybatis的CRUD-保存操作
  10. python 爬虫抓取网页数据导出excel_如何用excel实现网页爬虫
  11. 两道图片隐写的CTF题
  12. 使用Hexo + Gitee Pages搭建个人博客
  13. 直流电源EMI滤波器的设计
  14. Masking Adversarial Damage: Finding Adversarial Saliency for Robust and Sparse Network
  15. 把你的产品发到微店网上来,让190万微店(还在每天增加3万微店)为你免费推广!http://www.2226859.okwei.com/gy.html
  16. PTA——互评成绩(c语言)
  17. 【YOLO-Pose】在Windows上的部署与测试(调用摄像头)
  18. 冷门指标移中平均线和多空指数的完美结合(一定要看)
  19. 58同城笔试(2021/10/23)
  20. 在 github 上提交代码后,绿格子绿点没有显示

热门文章

  1. 刷题总结——Human Gene Functions(hdu1080)
  2. 【BZOJ 1031】[JSOI2007]字符加密Cipher(后缀数组模板)
  3. [DB]MariaDB 与 MySql 数据库
  4. Mininet的内部实现原理简介
  5. 浅析foreach原理
  6. python解释器的使用
  7. android_Media
  8. bzoj 2560: 串珠子【状压dp】
  9. 《A.I.爱》王力宏与人工智能谈恋爱 邀李开复来客串
  10. unity视频播放,