matlab分析xml文件

We can modify XML file in Java using DOM parser. We can add elements, remove elements, edit element values, edit attributes in an XML document in java using DOM Parser.

我们可以使用DOM解析器修改Java中的XML文件。 我们可以使用DOM Parser在Java中的XML文档中添加元素,删除元素,编辑元素值,编辑属性。

修改Java中的XML文件 (Modify XML File in Java)

Let’s say we have below source XML file. We will learn how to modify or edit this XML file in java program using DOM parser.

假设我们有下面的源XML文件。 我们将学习如何使用DOM解析器在Java程序中修改或编辑此XML文件。

employee.xml

employee.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Employees><Employee id="1"><name>Pankaj</name><age>29</age><role>Java Developer</role><gender>Male</gender></Employee><Employee id="2"><name>Lisa</name><age>35</age><role>CSS Developer</role><gender>Female</gender></Employee>
</Employees>

We will edit the XML file with below changes.

我们将通过以下更改来编辑XML文件。

  1. Update the “id” attribute value for all the Employee based on Gender. For Male, id will be prefixed with “M” else prefix with “F”.根据性别更新所有员工的“ id”属性值。 对于男性,id将以“ M”为前缀,否则以“ F”为前缀。
  2. Update the value of “name” element by making it to upper case.通过将大写的“名称”元素的值更新。
  3. Delete “gender” element as it’s not used now.删除“性别”元素,因为现在不使用它。
  4. Add a new element “salary” to all the employee node in the xml.向xml中的所有employee节点添加一个新元素“ salary”。

Once we make above modification to the XML, we will save it to a different file.

对XML进行以上修改后,我们会将其保存到其他文件中。

Here is the java program that does all the above updates using DOM Parser.

这是使用DOM分析器进行上述所有更新的Java程序。

ModifyXMLDOM.java

ModifyXMLDOM.java

package com.journaldev.xml;import java.io.File;
import java.io.IOException;import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;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 ModifyXMLDOM {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();//update attribute valueupdateAttributeValue(doc);//update Element valueupdateElementValue(doc);//delete elementdeleteElement(doc);//add new elementaddElement(doc);//write the updated document to file or consoledoc.getDocumentElement().normalize();TransformerFactory transformerFactory = TransformerFactory.newInstance();Transformer transformer = transformerFactory.newTransformer();DOMSource source = new DOMSource(doc);StreamResult result = new StreamResult(new File("employee_updated.xml"));transformer.setOutputProperty(OutputKeys.INDENT, "yes");transformer.transform(source, result);System.out.println("XML file updated successfully");} catch (SAXException | ParserConfigurationException | IOException | TransformerException e1) {e1.printStackTrace();}}private static void addElement(Document doc) {NodeList employees = doc.getElementsByTagName("Employee");Element emp = null;//loop for each employeefor(int i=0; i<employees.getLength();i++){emp = (Element) employees.item(i);Element salaryElement = doc.createElement("salary");salaryElement.appendChild(doc.createTextNode("10000"));emp.appendChild(salaryElement);}}private static void deleteElement(Document doc) {NodeList employees = doc.getElementsByTagName("Employee");Element emp = null;//loop for each employeefor(int i=0; i<employees.getLength();i++){emp = (Element) employees.item(i);Node genderNode = emp.getElementsByTagName("gender").item(0);emp.removeChild(genderNode);}}private static void updateElementValue(Document doc) {NodeList employees = doc.getElementsByTagName("Employee");Element emp = null;//loop for each employeefor(int i=0; i<employees.getLength();i++){emp = (Element) employees.item(i);Node name = emp.getElementsByTagName("name").item(0).getFirstChild();name.setNodeValue(name.getNodeValue().toUpperCase());}}private static void updateAttributeValue(Document doc) {NodeList employees = doc.getElementsByTagName("Employee");Element emp = null;//loop for each employeefor(int i=0; i<employees.getLength();i++){emp = (Element) employees.item(i);String gender = emp.getElementsByTagName("gender").item(0).getFirstChild().getNodeValue();if(gender.equalsIgnoreCase("male")){//prefix id attribute with Memp.setAttribute("id", "M"+emp.getAttribute("id"));}else{//prefix id attribute with Femp.setAttribute("id", "F"+emp.getAttribute("id"));}}}}

Output modified version of the XML file from above program is given below.

下面给出了来自上述程序的XML文件的输出修改版本。

employee_updated.xml

employee_updated.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Employees><Employee id="M1"><name>PANKAJ</name><age>29</age><role>Java Developer</role><salary>10000</salary>
</Employee><Employee id="F2"><name>LISA</name><age>35</age><role>CSS Developer</role><salary>10000</salary>
</Employee>
</Employees>

That’s all for a quick example of java edit XML file using DOM parser.

这就是使用DOM解析器的Java编辑XML文件的快速示例。

翻译自: https://www.journaldev.com/901/modify-xml-file-in-java-dom-parser

matlab分析xml文件

matlab分析xml文件_修改Java中的XML文件(DOM分析器)相关推荐

  1. java中 exe是什么文件_从文件位置运行Java中的.exe文件

    从文件位置运行Java中的.exe文件 我必须从我的Java程序打开一个.exe文件. 所以我试着下面的代码第一. Process process = runtime.exec("c:\\p ...

  2. java 获取文件时间_在java中怎么获取文件的最后修改日期

    在java中怎么获取文件的最后修改日期 发布时间:2020-07-10 09:57:26 来源:亿速云 阅读:86 作者:Leah 在java中怎么获取文件的最后修改日期?相信很多没有经验的人对此束手 ...

  3. java 空文件夹删除_删除Java中的空文件夹,处理隐藏文件

    我想删除java中的空文件夹,这里是我的代码: try (DirectoryStream stream = Files.newDirectoryStream(path)) { for (Path fi ...

  4. java 从excel中读取数据_在Java中读取Excel文件的内容和导出数据到Excel文件中

    转自www.chianjavaworld.net 原作者:SonyMusic 读:rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr 在Java ...

  5. java解析xml实例_在java中使用dom解析xml的示例分析

    本篇文章介绍了,在java中使用dom解析xml的示例分析.需要的朋友参考下 dom是个功能强大的解析工具,适用于小文档 为什么这么说呢?因为它会把整篇xml文档装载进内存中,形成一颗文档对象树 总之 ...

  6. cmd 将文件夹下文件剪切到另外一个文件_总结java中文件拷贝剪切的5种方式-JAVA IO基础总结第五篇...

    本文是Java IO总结系列篇的第5篇,前篇的访问地址如下: 总结java中创建并写文件的5种方式-JAVA IO基础总结第一篇 总结java从文件中读取数据的6种方法-JAVA IO基础总结第二篇 ...

  7. java使用缓冲区读取文件_在Java中使用Google的协议缓冲区

    java使用缓冲区读取文件 最近发布了 有效的Java第三版 ,我一直对确定此类Java开发书籍的更新感兴趣,该书籍的最新版本仅通过Java 6进行了介绍 . 在此版本中,显然存在与Java 7 , ...

  8. java文件和xml文件_用Java分割大型XML文件

    java文件和xml文件 上周,我被要求用Java编写一些东西,该东西能够将一个30GB的XML文件拆分为可配置文件大小的较小部分. 文件的使用者将是一个中间件应用程序,该应用程序在XML的大尺寸方面 ...

  9. jenkins修改pom文件_从Jenkins中的pom文件自动派生强制性SonarQube属性

    情况: 我想用由詹金斯(1.642.4)触发的SonarQube(5.4)分析我的项目.它是使用maven构建的Java项目. 我看到两种触发分析的方法: 发布构建操作"使用maven进行S ...

最新文章

  1. arcgis for js开发之路径分析
  2. svn版本库浏览器_svn:版本库xxx不存在||svn:No such revision xxx的问题
  3. Pyinstaller将Python程序打包成EXE(多种模式的打包)
  4. Vue method与computed的区别
  5. webpack2 项目构建一
  6. 仿头条新闻app,实现下拉刷新,上拉加载分页
  7. Confluence 6 导入 Active Directory 服务器证书 - Windows
  8. ios--小结系列三
  9. easyui省市二级联动
  10. CentOS安装jdk
  11. 安卓开发环境的搭建2017-01-15更新
  12. 手机APP支付--整合银联支付控件
  13. 特种浓缩分离:中药提取液的澄清过滤技术
  14. Elasticsearch APIs解析(一、API规范及约定)
  15. 关于HTTP常见状态码
  16. 人工智能----知识与知识表示
  17. linux开机出现initramfs无法进入系统
  18. 2020年中国程序员薪资和生活现状调查报告
  19. 2008 r2服务器日志文件,Windows2008R2共享文件访问日志查询的设置方法
  20. 掌握未来趋势的测试工程师成长之路

热门文章

  1. [转载] Python3 open()函数
  2. [转载] R语言read.table函数
  3. 谈谈使用Redis缓存时批量删除的几种实现
  4. 栈的应用--马踏棋盘-贪心加栈
  5. 自然语言处理真实项目实战(20170822)
  6. Java getResourceAsStream返回为空的问题
  7. php-学通PHP的24堂课-设置PHP的系统当前时间
  8. ffmpeg常用数据结构4
  9. 相机模型-Extended Unified Camera Model
  10. LabView学习笔记(二):滤波器实验