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. web网页前端制作中的SEO方法

    在SEO盛行的今天到处都在谈优化,对于网站前端制作人员来说,有几点是跟SEO相关 的,也就是SEO站内优化中的一部分,下面总结几点: 1.title,.页面的标题,不用多说,这个必须有! 2.keyw ...

  2. java.sql.SQLException: Io 异常: Connection refused(DESCRIPTION=(TMP=)(VSNNUM=186646784)(ERR=12505)(ERR

    dbc 链接orcal出错 java.sql.SQLException: Io 异常: Connection refused(DESCRIPTION=(TMP=)(VSNNUM=186646784)( ...

  3. Java集合框架的知识总结(1)

    说明:先从整体介绍了Java集合框架包含的接口和类,然后总结了集合框架中的一些基本知识和关键点,并结合实例进行简单分析. 1.综述 所有集合类都位于java.util包下.集合中只能保存对象(保存对象 ...

  4. [Gauss]POJ1222 EXTENDED LIGHTS OUT

    题意:给一个5*6的矩阵 1代表该位置的灯亮着, 0代表该位置的灯没亮 按某个位置的开关,可以同时改变 该位置 以及 该位置上方.下方.左方.右方, 共五个位置的灯的开.关(1->0, 0-&g ...

  5. MFC浅析(7) CWnd类虚函数的调用时机、缺省实现

    CWnd类虚函数的调用时机.缺省实现 FMD(http://www.fmdstudio.net) 1. Create 2. PreCreateWindow 3. PreSubclassWindow 4 ...

  6. 学习笔记-[Maven实战]-第三章:Maven使用入门(2)

    使用maven执行编译和测试 1.maven执行编译 (1).在pom.xml上点右键,选择Maven build... (2).在Goals里输入clean complie,执行编译 执行结果: [ ...

  7. 亚洲最佳电影TOP100出炉 你看过几部?

    亚洲最佳电影TOP100出炉 你看过几部?   在成立20周年之际,釜山国际电影节和釜山电影中心合作的Asian Cinema 100计划邀请亚洲电影领域较为权威的评论人和电影人共同评选出一张『100 ...

  8. bzoj2753

    第一问dfs不说 第二问很容易让人想到最小树形图,但是我不会,而且时间复杂度也不允许 还有什么不同的方法呢? 首先想到的是prim的思想,设根节点已经确定,其他点未确定 我们就不断从已确定的点延伸,找 ...

  9. win7无法识别U盘,驱动信息:该设备的驱动程序未被安装。 (代码 28)

    台式机的win7 64位系统可以识别u盘,但笔记本的win7 64位却识别不了,说明U盘是可以用的.查看笔记本的设备管理器,发现驱动安装失败,提示信息为“该设备的驱动程序未被安装. (代码 28) ” ...

  10. C# String.Format大全

    C# String.Format大全 ? ? ? 十进制的数字 ? ? string.Format("{0:D3}",23) 023 格式化十进制的数字 string.Format ...