IT程序员开发必备-各类资源下载清单,史上最全IT资源,个人收藏总结!

package edu.jaxp;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerConfigurationException;

import javax.xml.transform.TransformerException;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

import org.junit.Test;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

/**

* 测试对document对象进行CRUD操作

**/

public class JaxpDomCRUD {

//解析xml文档,得到代表文档的document

public Document getDocument(){

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = null;

Document document = null;

try {

builder = factory.newDocumentBuilder();

document = builder.parse("WebRoot/product1.xml");

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return document;

}

//将内存中修改的document对象更新至xml文档中

public void updateXML(Document documentSrc) {

//得到TransformerFactory实例对象

TransformerFactory transformerFactory = TransformerFactory.newInstance();

//由transformerFactory对象创建Transformer对象

Transformer transformer = null;

try {

transformer = transformerFactory.newTransformer();

} catch (TransformerConfigurationException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

/*

* 调用transformer对象的transform(Source xmlSource, Result outputTarget)方法

*  public class DOMSource extends Object implements Source

*      构造方法:DOMSource(Node n)

*  public class StreamResult extends Object implements Result

*      构造方法:StreamResult(OutputStream outputStream)

*/

try {

transformer.transform(new DOMSource(documentSrc),

new StreamResult(new FileOutputStream("WebRoot/product1.xml")));

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (TransformerException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

//向<product category="Table">节点中添加节点<notes>这是桌椅</notes>

@Test

public void appendNode(){

//得到xml文档的document对象

Document document = getDocument();

//创建<notes>新节点

Element notes = document.createElement("notes");

notes.setAttribute("name","肖华");

notes.setTextContent("这是桌椅");

//把创建的新节点追加在第二个<product>节点下

Element product = (Element) document.getElementsByTagName("product").item(1);

product.appendChild(notes);

//更新XML文档

updateXML(document);

}

//向xml文档中指定位置上添加节点:<notes>这是桌椅</notes>

@Test

public void insertNode(){

//得到xml文档的document对象

Document document = getDocument();

//创建<notes>新节点

Element notes = document.createElement("notes");

notes.setAttribute("name","肖华");

notes.setTextContent("这是桌椅");

//得到参考节点

Element refNode = (Element) document.getElementsByTagName("price").item(1);

//把创建的新节点插入在第二个<product>节点下的子节点<price>前面

Element product = (Element) document.getElementsByTagName("product").item(1);

product.insertBefore(notes, refNode);

//更新XML文档

updateXML(document);

}

//向xml文档指定节点添加或修改或删除属性

@Test

public void setOrUpdateAttr(){

//得到xml文档的document对象

Document document = getDocument();

//为 <notes>这是扳手</notes>节点添加属性

Element notesNode = (Element) document.getElementsByTagName("notes").item(0);

notesNode.setAttribute("name", "曹欢");

//为<price street="澳门街" wholesale="部分">100.0</price>修改属性值

Element priceNode = (Element) document.getElementsByTagName("price").item(1);

priceNode.setAttribute("wholesale", "小部分");

// 移除<specifications weight="2.0kg">扳手</specifications>的weight属性

Element specificationsNode = (Element) document.getElementsByTagName("specifications").item(0);

specificationsNode.removeAttribute("weight");

//更新XML文档

updateXML(document);

}

//删除xml文档中指定的节点<notes name="曹欢">这是扳手</notes>

@Test

public void deleteNode(){

//得到xml文档的document对象

Document document = getDocument();

//取得要删除的节点

Element notesNode = (Element) document.getElementsByTagName("notes").item(0);

//取得要删除的节点的父节点,如果noteNode节点为null的话,则会抛出NullPointerException

//Element parentNode = (Element) notesNode.getParentNode();

Element parentNode = (Element) document.getElementsByTagName("product").item(1);

//由父节点移除子节点

parentNode.removeChild(notesNode);

//更新XML文档

updateXML(document);

}

//修改Element元素的内容

@Test

public void updateContextText(){

//得到xml文档的document对象

Document document = getDocument();

//priceNode只能是productNode节点的子节点

Element productNode = (Element) document.getElementsByTagName("product").item(0);

Element priceNode = (Element) productNode.getElementsByTagName("price").item(0);

priceNode.setTextContent("90.0");

//更新XML文档

updateXML(document);

}

}

product1.xml文件内容:

<?xml version="1.0" encoding="utf-8" standalone="no"?>

<catalog id="cata1">

<product category="HandTool">

<specifications>扳手</specifications>

<price street="香港街">90.0</price>

</product>

<product category="Table">

<specifications>桌椅</specifications>

<notes name="肖华">这是桌椅</notes>

<price street="澳门街" wholesale="小部分">100.0</price>

</product>

</catalog>

用“XML解析开发包Jaxp”对XML文件进行Dom方式的CRUD操作相关推荐

  1. java自带的xml解析_Java自带的XML解析

    JAXP(Java API for XML Processing,意为处理XML的Java API) 解析XML一般有两种方式: 一种是DOM方式:一次性读取XML内容存入内存 优点:能进行各种增删改 ...

  2. 【ESP8266】ESP8266_NONOS_SDK开发包生成的镜像文件构建步骤分析

    ESP8266有官方提供的软件开发包.下面是对该开发包ESP8266_NONOS_SDK生成的镜像文件构建步骤分析. 一.Flash布局 首先参考官方提供编号为2A的文档,对于4MB(32Mbit) ...

  3. 使用JAXP对xml文档进行DOM解析基础

    XML解析方式分为两种:dom和sax         dom:(Document Object Model, 即文档对象模型) 是 W3C 组织推荐的处理 XML 的一种方式.        sax ...

  4. Java基础加强重温_13:XML(可拓展标记语言)、XML语法、XML约束、XML解析(Dom4j,JAXP)、Dom4j基本使用、Xpath表达式(XML路径语言)、XML解析综合案例

    摘要 Java基础加强重温_13: XML(可拓展标记语言.作用:小型数据库.框架配置文件.不同平台数据交换). XML语法(文档.标签/元素.属性.注释.转义字符.CDTA区) XML约束(DTD约 ...

  5. xml解析: dom4j

    目录 一.XML解析 1.概述 2.解析方式和解析器 二.Dom4j的基本使用 1.解析原理 2.基本使用 2.常用方法 (1)SaxReader对象 (2)Document对象 (3)Element ...

  6. XML解析技术简介——(一)

    解析技术(两种) 基本的解析方式有两种,一种叫SAX,另一种叫DOM.SAX是基于事件流的解析,DOM是基于XML文档树结构的解析. DOM:document object model   W3C组织 ...

  7. xml解析总结-常用需掌握

    Xml文档的解析 XML解析方式分为两种:DOM方式和SAX方式 DOM:Document Object Model, 文档对象模型.这种方式是W3C推荐的处理XML的一种方式. SAX:Simple ...

  8. 使用JAXP对XML文档进行DOM解析

    一.XML解析方式分为两种:dom和sax dom:(Document Object Model,即文档对象模型)是W3C组织推荐的解析XML的一种方式. sax:(Simple API for XM ...

  9. XML解析方式(来自 传智播客 方立勋视频教程)

    为什么80%的码农都做不了架构师?>>>    XML解析方式一般有两种:DOM和SAX DOM:(Document Object Model,即文档对象模型)是W3C组织推荐的解析 ...

最新文章

  1. c库的rand/random随机数产生函数性能差?
  2. 德勤:2025年汽车行业价值链的四个合理情境
  3. 十年——透过BILL的眼睛
  4. 129. 火车进栈【栈】
  5. 11.2.5 属性
  6. Windows 11 上大招!正式支持安卓!
  7. 每日一笑 | 谷歌能严谨到什么地步?
  8. Springboot 整合jsp案例
  9. 自学前端,需要学习哪些知识点?学多久可以入职前端工程师?
  10. Asp.Net高级知识回顾_HttpModule及应用程序生命周期_1
  11. 计算机系统安装要点,电脑重新装系统要注意哪些要点 重装系统时的六大事项...
  12. 快去抢票!今天开始!2020元旦春节火车票购票日程攻略来了
  13. 破解老程序员的迷茫病——JUST DO IT
  14. 机器学习03:人工神经网络
  15. 线程池系列三:动态修改线程池队列大小
  16. 利用Opencv+Qt打开摄像头
  17. 【翻译】使用Sencha Ext JS创建美丽的图画(1)
  18. 第七章 突变和随机化
  19. kali linux外网渗透指定ip,kali Linux局域网渗透之win10
  20. JavaScript中原生Array数组方法详解

热门文章

  1. unity2D横版游戏教程9-对话框dialog
  2. 【数据库修复】.Globeimposter-Alpha66qqz勒索病毒的数据库文件修复
  3. Android快速入门(一):Android介绍
  4. 公司招了个五年经验的测试员,见识到了真正的测试天花板
  5. 辣鸡公司联动优势,你在毕业季给应届毕业生解约的操作真TM丑陋
  6. Java中常见的几种任务调度框架对比
  7. DSA-数据签名算法(理论)
  8. 基于springboot企业客户信息反馈平台设计与实现的源码+文档
  9. AI绘画神器Stable Diffusion的疯狂与危险
  10. [转贴]Symbian开发入门 - UIQ开发教程