Java dom4j如何获取,添加,删除,查找,设置Element属性呢?
转自:
Java dom4j如何获取,添加,删除,查找,设置Element属性呢?
下文讲述dom4j操作Element属性的方法分享,如下所示:
取得某节点下的某属性
Element root=document.getRootElement(); Attribute attribute=root.attribute("size");// 属性名name
取得属性的文字
String text=attribute.getText(); // 这个是取得根节点下name字节点的属性firstname的值: String text2=root.element("name").attributeValue("firstname");
遍历某节点的所有属性
Element root=document.getRootElement(); for(Iterator it=root.attributeIterator();it.hasNext();){ Attribute attribute = (Attribute) it.next(); String text=attribute.getText(); System.out.println(text); }
设置某节点的属性和文字
newMemberElm.addAttribute("name", "属性值");
设置属性的文字
Attribute attribute=root.attribute("name"); attribute.setText("属性值");
删除某属性
Attribute attribute=root.attribute("name");// 属性名name root.remove(attribute);
例
public void modifyDoc() {try {Document doc = reader.read(new File("file/test.xml"));//修改属性内容List list = doc.selectNodes("//article/@level");Iterator<Attribute> iter = list.iterator();while (iter.hasNext()) {Attribute attr = iter.next();fail(attr.getName() + "#" + attr.getValue() + "#" + attr.getText());if ("Intermediate".equals(attr.getValue())) {//修改属性值attr.setValue("新属性值");fail(attr.getName() + "#" + attr.getValue() + "#" + attr.getText());}}list = doc.selectNodes("//article/@date");iter = list.iterator();while (iter.hasNext()) {Attribute attr = iter.next();fail(attr.getName() + "#" + attr.getValue() + "#" + attr.getText());if ("我是属性值".equals(attr.getValue())) {//修改属性值attr.setValue("新属性值");fail(attr.getName() + "#" + attr.getValue() + "#" + attr.getText());}}//写入到文件/*XMLWriter output = new XMLWriter(new FileWriter(new File("file/test.xml")));output.write(doc);output.close();*/} catch (DocumentException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();} }
Java dom4j如何获取,添加,删除,查找,设置Element属性呢?相关推荐
- Java如何使用dom4j获取,添加,删除,查找,设置Element节点呢?
转自: Java如何使用dom4j获取,添加,删除,查找,设置Element节点呢? 下文笔者讲述DOM4J操作Element节点的示例分享,如下所示 获取文档的根节点 Element rootElm ...
- ArcGis国土空间”一张图”填报操作技巧(四)零编程基础批量添加字段并设置字段属性
向表或要素类表.要素图层和/或带属性表的栅格添加字段并设置字段属性,是我们在数据建库或处理时常常要用到的操作,实现的方法非常多. 一.逐一添加字段的常见操作 1.打开属性表--添加字段. 2.使用字段 ...
- java反射无法获取_Java反射'无法设置'错误
我正在尝试使用Java反射来获取通用Field属性的实例,以便执行此Field的方法. 例如,如果我使用getValue()方法创建类型为MyType的类,并且我有另一个具有MyType类属性的MyC ...
- 文档对象模型DOM(获取元素节点、设置节点属性)
练习题: 制作一个表格,显示班级的学生信息. 要求: 1. 鼠标移到不同行上时背景色改为色值为 #f2f2f2,移开鼠标时则恢复为原背景色 #fff 2. 点击添加按钮,能动态在最后添加一行 3. 点 ...
- java removeattribute_JS removeAttribute()方法实现删除元素的某个属性
在 JavaScript 中,使用元素的 removeAttribute() 方法可以删除指定的属性.用法如下: removeAttribute(name) 参数 name 表示元素的属性名. 示例1 ...
- electron获取cookie删除cookie设置cookie
const {session} = require('electron') //获取cookie session.defaultSession.cookies.get({url:'要获取的地址'},( ...
- Cookie的设置获取和删除
关于Cookie操作的实验,包括设置,获取和删除. 设置Cookie name[必选] value[必选] expiredays[必选] setCookie getCookie 操作Cookie GE ...
- JQuery 设置删除属性 设置删除class 设置删除css
1.设置删除属性 $('#img').attr("src", "./images/img1.jpg"); $('#img').removeAttr(" ...
- java生成带书签的pdf,Java 添加、更新、获取、删除PDF中的书签
Spire.Cloud.SDK for Java WebAPI提供了pdfBookmarkApi接口可用于添加书签addBookmark().更新书签updateBookmark().获取书签信息ge ...
最新文章
- ORACLE查询删除重复记录
- Python中关于使用正则表达式相关的部分笔记
- C++实现有序表折半查找
- 通过Canvas在浏览器中更酷的展示视频
- 一文入门 Kafka
- ITTC数据挖掘平台介绍(综述)——平台简介
- 嵌入式面试中常见的编程题目
- -9 逆序输出一个整数的各位数字_【Java编程基本功】(八)逆序输出、是否为回文数,判断星期几,升序排列...
- Handler BlockViewHandler has a bad module ManagedPipelineHandler in its module list
- Linux linux下的进程状态
- gitlab和jenkins发布代码
- python应聘的职位_Python学到哪种程度可以去应聘相关的职位了?
- python拆分字符串函数_Python字符串操作-拆分,切片,从HEX转换为DEC
- Javascript 事件入门
- 推荐一款.NET开源免费的图表控件ScottPlot
- 月薪5000和年薪百万的差距在哪里?
- Linux面试常考命令
- html js动态时间轴,jQuery时间轴插件timeline.js
- Publish over SSH 插件配置
- 网易公开课付费视频没有加密,可以随意下载到本地