package com.soft.common;

import java.util.HashMap;
import java.util.Map; import org.jdom2.Namespace; /**
* 节点操作属性封装
* @author xingxf
*
*/
public class Nodes { //文件路径
private String path; //节点名称
private String NodeName; //xml命名空间
private Namespace nameSpace; //节点文本值
private String content; //节点属性名 key 节点属性值 value
private Map<String, String> attribute=new HashMap<String, String>(); public String getPath() {
return path;
} public void setPath(String path) {
this.path = path;
} public Nodes() {
super();
// TODO Auto-generated constructor stub
} public Nodes(String path, String nodeName, Namespace nameSpace,
String content, Map<String, String> attribute) {
super();
this.path = path;
NodeName = nodeName;
this.nameSpace = nameSpace;
this.content = content;
this.attribute = attribute;
} public Nodes(String nodeName, String content, Map<String, String> attribute) {
super();
NodeName = nodeName;
this.content = content;
this.attribute = attribute;
} public String getNodeName() {
return NodeName;
}
public void setNodeName(String nodeName) {
NodeName = nodeName;
}
public Namespace getNameSpace() {
return nameSpace;
}
public void setNameSpace(Namespace nameSpace) {
this.nameSpace = nameSpace;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
} public Map<String, String> getAttribute() {
return attribute;
} public void setAttribute(Map<String, String> attribute) {
this.attribute = attribute;
} public Map<String, String> setmap(String key,String value){
Map<String, String> map=new HashMap<String, String>();
map.put(key, value);
return map;
} }

  

package com.test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.Namespace;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter; import com.soft.common.Nodes; public class XmlOperationUtils { public static void main(String[] args) {
try {
XmlOperationUtils dj = new XmlOperationUtils(); String file = "D:/Tomcat/webapps/oagovern/WEB-INF/flowtemplate/dd.jpdl.xml";// 设置文件路径和名称;
Nodes nodes=new Nodes();
nodes.setPath(file);
nodes.setNodeName("persons");
nodes.setNameSpace(Namespace.getNamespace("http://jbpm.org/4.4/jpdl"));
Map<String, String> attr=new HashMap<String, String>();
attr.put("name", "test1");
attr.put("type", "String");
attr.put("key", "test");
attr.put("value", "value");
nodes.setAttribute(attr);
nodes.setContent("小明学习Java"); //初始化xml
dj.initXmlNode(nodes); //获取根节点
Document document = dj.getRootElement(nodes.getPath());
Element root=dj.getRootNode(document); //添加子级节点
Nodes childNode=new Nodes("person", "test", nodes.setmap("name", "hello"));
Element child =dj.createChildElement(root,childNode); //添加子级的子级
Nodes childNodes=new Nodes("student", "小明学习Java", nodes.setmap("type", "int"));
dj.createChildElement(child,childNodes); // 生成xml
dj.StoreXmlToFile(document, nodes.getPath()); //解析xml
dj.parserXml(file); } catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } /**
*
* @param filepath 文件路径
*/
public void createXml(String filepath) {
Document document = new Document();
Namespace ns= Namespace.getNamespace("http://jbpm.org/4.4/jpdl");
Element root = new Element("persons",ns).setAttribute("name", "persons");
document.setRootElement(root);
createChildElements(root);
StoreXmlToFile(document, filepath);
} /**
* 初始化xml根节点和文件
* @param nodes
*/
public boolean initXmlNode(Nodes nodes){
boolean flag=false;
if (nodes!=null) { Document document=new Document(); Element root=new Element(nodes.getNodeName(),nodes.getNameSpace());
Map<String, String> map=nodes.getAttribute();
for (String key:map.keySet()) {
System.out.println("key:"+key+" and value:"+map.get(key));
root.setAttribute(key,map.get(key));
} document.setRootElement(root); StoreXmlToFile(document, nodes.getPath());
flag=true;
} return flag;
} /**
* 获取根节点
* @param document 根据路径获得的文档
* @return 根节点
*/
public Element getRootNode(Document document){
return document.getRootElement();
} /**
* 创建xml子节点
* @param parent 父节点
* @param nodes 节点实例对象
*/
private Element createChildElement(Element parent ,Nodes nodes) {
Element child=new Element(nodes.getNodeName());
Map<String, String> map=nodes.getAttribute();
for(String key:map.keySet()){
child.setAttribute(key,map.get(key));
}
setChildElementNS(child,parent);
child.setText(nodes.getContent());
parent.addContent(child);
return child; } /**
* 设置子级元素的命名空间 可以实现去除子节点命名空间
* @param child ChildrenElement
* @param root RootElement
*/
private void setChildElementNS(Element child, Element root) {
child.setNamespace(root.getNamespace());
} /**
* 生成xml文件
* @param document xml文档
* @param fliePath 文件路径
*/
private void StoreXmlToFile(Document document, String fliePath) {
XMLOutputter XMLOut = new XMLOutputter();
try {
Format f = Format.getPrettyFormat();
f.setEncoding("UTF-8");// default=UTF-8
XMLOut.setFormat(f);
XMLOut.output(document, new FileOutputStream(fliePath));
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 添加xml文件节点内容
* @param root 根节点
*/
private void createChildElements(Element root) { //在root节点下创建子节点
Element person = new Element("person");
person.setNamespace(root.getNamespace());
root.addContent(person); Element name = new Element("name");
name.setText("java小强");
name.setNamespace(root.getNamespace());
person.addContent(name); Element sex = new Element("sex");
sex.setText("man");
sex.setNamespace(root.getNamespace());
person.addContent(sex); Element start=new Element("start").setAttribute("name", "satrt1");
start.setAttribute("class", "start");
start.setNamespace(root.getNamespace());
root.addContent(start); Element trations=new Element("trations");
setChildElementNS(trations, root);
start.addContent(trations); Element heand=new Element("heand");
heand.setText("Java操作");
setChildElementNS(heand, root);
trations.addContent(heand); } /**
* 根据路径获取xml文档
* @param file 文件路径
*/
public Document getRootElement(String file) throws JDOMException, IOException{
SAXBuilder builder=new SAXBuilder();
return builder.build(file);
} /**
* 解析XML
* @param filePath 文件路径
*/
public void parserXml(String file) {
try {
Document document=getRootElement(file);
Element root = document.getRootElement();
List persons = root.getChildren("person");
for (int i = 0; i < persons.size(); i++) {
Element person = (Element) persons.get(i);
List pros = person.getChildren();
for (int j = 0; j < pros.size(); j++) {
Element element = (Element) pros.get(j);
System.out.println(element.getName() + ":" + element.getValue());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

  jdom-2.0.6.jar

解决思路:

  理论:当命名空间被定义在元素的开始标签中时,所有带有相同前缀的子元素都会与同一个命名空间相关联。

  方法:为该xml的子节点添加与父节点相同的命名空间方法 可以去除子节点含有命名空间

下载链接

使用jdom操作xml文件 去除子节点带有命名空间的更多相关文章

  1. 摘抄的 JAVA JDOM 操作XML文件

    JDOM修炼篇 用过XERCES的程序员都会感到,有时候用一句话就可以说清楚的事,当用XERCES的API来实现时,要三四行程序.   回页首 获得并安装JDOM 在 http://www.jdom. ...

  2. C#中操作xml文件(插入节点、修改、删除)

    已知有一个xml文件(bookstore.xml)如下: <?xml version="1.0" encoding="gb2312"?> <b ...

  3. Java文件操作①——XML文件的读取

    一.邂逅XML 文件种类是丰富多彩的,XML作为众多文件类型的一种,经常被用于数据存储和传输.所以XML在现今应用程序中是非常流行的.本文主要讲Java解析和生成XML.用于不同平台.不同设备间的数据 ...

  4. JDOM 操作XML

    http://www.cnblogs.com/hoojo/archive/2011/08/11/2134638.html 可扩展标记语言——eXtensible Markup Language 用户可 ...

  5. WebAPI调用笔记 ASP.NET CORE 学习之自定义异常处理 MySQL数据库查询优化建议 .NET操作XML文件之泛型集合的序列化与反序列化 Asp.Net Core 轻松学-多线程之Task快速上手 Asp.Net Core 轻松学-多线程之Task(补充)

    WebAPI调用笔记   前言 即时通信项目中初次调用OA接口遇到了一些问题,因为本人从业后几乎一直做CS端项目,一个简单的WebAPI调用居然浪费了不少时间,特此记录. 接口描述 首先说明一下,基于 ...

  6. Asp.Net 操作XML文件的增删改查 利用GridView

    不废话,直接上如何利用Asp.NET操作XML文件,并对其属性进行修改,刚开始的时候,是打算使用JS来控制生成XML文件的,但是最后却是无法创建文件,读取文件则没有使用了 index.aspx 文件 ...

  7. C#程序中:如何修改xml文件中的节点(数据)

    要想在web等程序中实现动态的数据内容给新(如网页中的Flash),不会更新xml文件中的节点(数据)是远远不够的,今天在这里说一个简单的xml文件的更新,方法比较基础,很适合初学者看的,保证一看就懂 ...

  8. Java操作XML文件 dom4j 篇

    在项目中,我们很多都用到了xml文件,无论是参数配置还是与其它系统的数据交互.今天就来讲一下Java 中使用dom4j来操作XML文件. 我们需要引入的包: //文件包 import java.io. ...

  9. PHP操作XML文件学习笔记

    原文:PHP操作XML文件学习笔记 XML文件属于标签语言,可以通过自定义标签存储数据,其主要作用也是作为存储数据. 对于XML的操作包括遍历,生成,修改,删除等其他类似的操作.PHP对于XML的操作 ...

随机推荐

  1. C#压缩、解压缩文件(夹)(rar、zip)

    主要是使用Rar.exe压缩解压文件(夹)(*.rar),另外还有使用SevenZipSharp.dll.zLib1.dll.7z.dll压缩解压文件(夹)(*.zip).需要注意的几点如下: 1.注 ...

  2. loadrunner 脚本和replaylog中的中文乱码问题(转载)

    解决这个问题必须认识到一个事实就是,loadrunner和测试服务器交换数据使用的是utf8格式,但是展现在replaylog中是使用gb2312格式,而且在脚本中如何使用web_reg_find的时 ...

  3. 李洪强iOS开发之-环信04_消息

    李洪强iOS开发之-环信04_消息 消息:IM 交互实体,在 SDK 中对应的类型是 EMMessage.EMMessage 由 EMMessageBody 组成. 构造消息   构造文字消息 EMT ...

  4. vcastr2.0插件超级简单使用

                Vcastr2.0简单使用 友情提示:1.蓝色文字为必修改内容.2.#字符后面是解释该代码段的主要内容 1. 引用swfobject.js文件 #public/videoplu ...

  5. QT中的字符串处理函数

    Fn 1 : arg 这个函数的具体声明不写了,它有20个重载,典型的示例代码如下: 1: #include <QtCore/QCoreApplication> 2: #include & ...

  6. 启动Activity时显示空白界面的问题

    问题描述: 启动activity时,先显示一个空白的界面,带标题栏的,1秒左右的时间后才显示activity对应 layout上的内容. 解决办法: 将activity的windows设置为透明的就可 ...

  7. 近期会放出tlplayer for android的更新版本

    tlplayer for android的一次重大更新在近期将会放出,自从去年初的时候放出tlplayer android版本后,一直都没有更新tlplayer,而tlplayer for windo ...

  8. Import Items – Validation Multiple Languages Description

            ð  提交标准请求创建和更新物料,因语言环境与处理次序方式等因素,造成物料中英(更多语言)描述和长描述混乱刷新. 症状: >>> Submit Standard Op ...

  9. iis 5.1 连接 sql 2005

    VS2005+SQL2005 ASP.NET2.0数据库连接总结 通过上篇文章(http://www.cnblogs.com/user34j/archive/2007/01/23/628426.htm ...

  10. 从头开始编写一个Orchard网上商店模块(4) - 创建ProductPart

    原文地址:http://skywalkersoftwaredevelopment.net/blog/writing-an-orchard-webshop-module-from-scratch-par ...