jdom使用入门及namespace注意事项【原】
报文样例
<person:info xmlns:person="http://person/abc" id="1"> <fruit id="2">
<apple id="3" color="red" size="big" >good apple</apple>
</fruit> <person:pets id="4"> <cat:pet xmlns:cat="http://cat/def" id="5" type="cat">
<person:master id="6">King</person:master>
<cat:age id="7">1</cat:age>
<cat:color id="8">white</cat:color>
<happy id="9">true</happy>
</cat:pet> <dog:pet xmlns:dog="http://dog/ghi" id="10" type="dog">
<person:master id="11">King</person:master>
<dog:age id="12">2</dog:age>
<dog:color id="13">white</dog:color>
<happy id="14">false</happy>
</dog:pet> </person:pets> </person:info>
jdom样例
package d; import java.io.File;
import java.util.List; import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter; public class JdomDemo { @SuppressWarnings({ "unused", "unchecked" })
public static void main(String[] args) throws Exception{
File file = new File("C:/Users/35992/Desktop/a.xml");
Document doc = (new SAXBuilder()).build(file);
Element root = doc.getRootElement();
Namespace rootNS = root.getNamespace(); Element fruit = root.getChild("fruit");//取单个直接孩子节点
Element apple =fruit.getChild("apple");//取单个直接孩子节点
List<Attribute> attrList = apple.getAttributes();
for(int i = 0 ; i < attrList.size() ; i++){//遍历apple 的3个属性
String appleAttrValue = attrList.get(i).getValue();
System.out.println(appleAttrValue);
} //<person:pets id="4"> 有冒号需要添加person的Namespace,不添加会找不到,返回null
Element petNull = root.getChild("pets");
//<person:pets id="4"> 有冒号需要添加person的Namespace
Element petNotNull = root.getChild("pets",Namespace.getNamespace("person", "http://person/abc")); Element pets = root.getChild("pets", rootNS);//取单个直接孩子节点 List<Element> petList = pets.getChildren();//取所有直接孩子节点们
for(int i = 0 ; i < petList.size(); i++){//pets有2个pet子节点
Element pet = petList.get(i); if("cat".equals(pet.getAttributeValue("type"))){
Element masterNull = pet.getChild("master");
Element ageNull = pet.getChild("age");
Element colorNull = pet.getChild("color");
Element happy = pet.getChild("happy");
System.out.println(masterNull+" - " + ageNull+" - " + colorNull+" - " + happy);
} if("dog".equals(pet.getAttributeValue("type"))){
//<person:master id="11">King</person:master> 有冒号需要添加person的Namespace
Element master = pet.getChild("master",Namespace.getNamespace("person", "http://person/abc"));
//<dog:age id="12">2</dog:age> 有冒号需要添加dog的Namespace
Element age = pet.getChild("age",Namespace.getNamespace("dog", "http://dog/ghi"));
//<dog:color id="13">white</dog:color> 有冒号需要添加dog的Namespace
Element color = pet.getChild("color",Namespace.getNamespace("dog", "http://dog/ghi"));
Element happy = pet.getChild("happy");//<happy id="14">false</happy> 没有冒号,直接取
System.out.println(master+" - " + age+" - " + color+" - " + happy);
}
} //格式化输出xml文件字符串
Format format = Format.getCompactFormat().setEncoding("utf-8").setIndent("\t");
XMLOutputter xmlout = new XMLOutputter(format);
String result = xmlout.outputString(doc);
System.out.println(result);
} }
参考
XML 命名空间(XML Namespaces)--http://www.w3school.com.cn/xml/xml_namespaces.asp
jdom使用入门及namespace注意事项【原】的更多相关文章
- k8s入门之namespace(三)
namespace的作用就是用来隔离资源,将同一集群中的资源划分为相互隔离的组.同一名称空间内的资源名称要唯一,但不同名称空间时没有这个要求.有些k8s资源对象与名称空间没有关系,例如 Storage ...
- NuGet程序包安装SQLite后完全抽离出SQLite之入门介绍及注意事项,你真的懂了吗?
前言 近几天的几篇文章讲的内容非前面内容如系列的讲解,这几天文章都是我在项目中遇到的问题以及重新学习的知识,所以和大家分享一下,关于SQLite的文章多如牛毛,但是有些大多已经过时,为什么说过时,之前 ...
- Spring AOP入门——概念和注意事项
AOP什么? AOP在功能方面,它是之前和之后运行一些业务逻辑,一些操作(比方记录日志.或者是推断是否有权限等),这些操作的加入.全然不耦合于原来的业务逻辑.从而对原有业务逻辑全然是透明. 也就是说. ...
- 【dp入门题】【跟着14练dp吧...囧】
A HDU_2048 数塔 dp入门题——数塔问题:求路径的最大和: 状态方程: dp[i][j] = max(dp[i+1][j], dp[i+1][j+1])+a[i][j];dp[n][j] = ...
- Android Navigation 架构组件入门教程
Android Navigation 架构组件入门教程 版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请表明出处:https://www.cnblogs.com/cavalier-/p/1 ...
- Dbutils学习(介绍和入门)
一:Dbutils是什么?(当我们很难理解一个东西的官方解释的时候,就让我们记住它的作用) Dbutils:主要是封装了JDBC的代码,简化dao层的操作. 作用:帮助java程序 ...
- C++中对C的扩展学习新增语法——namespace
NAMESPACE语法 namespace主要解决了命名冲突的问题,语法如下 Namespace注意事项: namespace中可以定义常量.变量.函数.结构体.枚举.类等. namespace 只能 ...
- 【DWR系列02】-DWR逆向Ajax即服务器推送
.literal { background-color: #f2f2f2; border: 1px solid #cccccc; padding: 1px 3px 0; white-space: no ...
- BOOST 线程完全攻略 - 基础篇
http://blog.csdn.net/iamnieo/article/details/2908621 2008-09-10 12:48 9202人阅读 评论(3) 收藏 举报 thread多线程l ...
随机推荐
- 【XSY2731】Div 数论 杜教筛 莫比乌斯反演
题目大意 定义复数\(a+bi\)为整数\(k\)的约数,当且仅当\(a\)和\(b\)为整数且存在整数\(c\)和\(d\)满足\((a+bi)(c+di)=k\). 定义复数\(a+bi\)的实部 ...
- hdu 5877 Weak Pair (Treap)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5877 题面; Weak Pair Time Limit: 4000/2000 MS (Java/Other ...
- 【java+selenium】网易云音乐刷累计听歌数
背景应该是在去年的时候,刷知乎看到一个问题,大概是说怎么刷网易云音乐个人累计听歌数,然后有一个高赞回答,贴了一段js代码,直接在浏览器console执行就可以了.当时试了下,直接一下子刷了有好几万.悲 ...
- rt-thread 学习路线
@2019-01-25 [小记] > BSP制作与使用 将板载资源.芯片外设全部制作BSP,做到使用时只需在 menuconfig 中配置即用
- emwin之WM_DeleteWindow函数使用注意事项
@2018-12-21 [小记] 在当前窗口P创建一新窗口C后再使用函数 WM_DeleteWindow 删除该C窗口时,删除窗口句柄必须是根句柄,如果使用 WM_GetClientWindow(pM ...
- luogu3621 城池攻占 (倍增)
好像所有人都写的左偏树 但我不会啊233 首先发现乘的时候 系数不会为负,所以能得到一个关键条件:变化后的战斗力随变化前的战斗力大小单调 所以我们考虑倍增 设hp[x][i]是从x开始一路攻克$2^i ...
- POJ-3436 ACM Computer Factory(网络流EK)
As you know, all the computers used for ACM contests must be identical, so the participants compete ...
- 各种“地”—— 各种“GND”
GND,指的是电线接地端的简写.代表地线或0线. 电路图上和电路板上的GND(Ground)代表地线或0线.GND就是公共端的意思,也可以说是地,但这个地并不是真正意义上的地.是出于应用而假设的一个地 ...
- Rocket.Chat 开源IM系统部署
Rocket.Chat 官方给出的文档也个人觉得太麻烦了,并且对ubuntu的支持程度远高于CentOS,自己就折腾写了个安装的笔记,如果是在公司内部或者是部门内部还是很有用处的,比较看中的功能有和g ...
- 2017蓝桥杯 省赛D题(方格分割)
6x6的方格,沿着格子的边线剪开成两部分.要求这两部分的形状完全相同. 如图:p1.png, p2.png, p3.png 就是可行的分割法. 试计算:包括这3种分法在内,一共有多少种不同的分割 ...