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 ...
随机推荐
- 洛谷P1092 虫食算(算竞进阶习题)
模拟+dfs 这个题就三行,搜索的话我们从右向左,从上到下.. 如果是在1,2行我们就直接枚举0-n所有数,但是到了第三行,最直接的就是填上这一列上前两行的数的和modN,在此基础上判断该填的数有没有 ...
- 【XSY2718】gift 分数规划 网络流
题目描述 有\(n\)个物品,买第\(i\)个物品要花费\(a_i\)元.还有\(m\)对关系:同时买\(p_i,q_i\)两个物品会获得\(b_i\)点收益. 设收益为\(B\),花费为\(A\), ...
- 【CF734F】Anton and School(构造)
[CF734F]Anton and School(构造) 题面 Codeforces 洛谷 题解 算是一道\(easy\)? 发现\((a\&b)+(a|b)=a+b\). 那么根据给定条件我 ...
- [CF1107E]Vasya and Binary String【区间DP】
题目描述 Vasya has a string s of length n consisting only of digits 0 and 1. Also he has an array a of l ...
- [SCOI2007]压缩(区间dp)
神仙题,看了半天题解才看明白... 因为题目里说如果没有m,会自动默认m在最前面. 我们设计状态为dp[l][r][0/1]为在区间l到r中有没有m的最小长度. 转移:枚举我们要压缩的起点,dp[l] ...
- 20165223 结对编程之四则运算week1-阶段性总结
目录 一.结对对象 二.需求分析 三.设计思路 四.功能截图 五.结对感受 一.结对对象 担任角色 驾驶员(Driver):20165223 蔡霓(是控制键盘输入的人) 领航员(Navigator): ...
- Codeforces Round #513 by Barcelona Bootcamp (rated, Div. 1 + Div. 2) C D
C - Maximum Subrectangle 因为是两个数组相乘的到的 矩阵所以 a(i ->j)*b(x->y) 的面积 就是 a(i ->j) 的和乘与b(x-> ...
- [WC2018]州区划分
[WC2018]州区划分 注意审题: 1.有序选择 2.若干个州 3.贡献是州满意度的乘积 枚举最后一个州是哪一个,合法时候贡献sum[s]^p,否则贡献0 存在欧拉回路:每个点都是偶度数,且图连通( ...
- JavaServer Faces (JSF) with Spring
JavaServer Faces (JSF) with Spring Last modified: April 30, 2018 by baeldung Spring+ Spring MVC JSF ...
- [CTSC2018]暴力写挂——边分树合并
[CTSC2018]暴力写挂 题面不错 给定两棵树,两点“距离”定义为:二者深度相加,减去两棵树上的LCA的深度(深度指到根节点的距离) 求最大的距离. 解决多棵树的问题就是降维了. 经典的做法是边分 ...