xml文档中的常见节点:
import javax.xml.parsers.*;
import java.io.*;
import org.w3c.dom.*;
public class code10_3 {
static Document document;
public static void main(String args[]){
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try{
DocumentBuilder db = dbf.newDocumentBuilder();
//读取xml文档
document = db.parse(new File("G:\\EclipseWorkPlace\\test.xml"));
//获得根元素
Node root = document.getDocumentElement();
System.out.println("根节点的名字是:" + root.getNodeName());
//获得根元素的子节点列表
NodeList childs = root.getChildNodes();
System.out.println("子节点的长度是:" + childs.getLength());
getElement(childs);
}catch (Exception e){
System.out.println(e);
}
}
public static void getElement(NodeList childs){
int i=0;
if (childs.getLength() == 0){
//该孩子没有子节点
System.out.println("该孩子没有子节点!");
return;
}
for (i=0; i<childs.getLength(); i++){
//取得第i个子节点
Node node = childs.item(i);
//获取节点的类型,可以是ElementNode,TextNode,DocumentNode等
short nodetype = node.getNodeType();
/*ElementNode类型的节点可以包含子节点和属性等*/
if (nodetype == Node.ELEMENT_NODE){
//得到节点的名称
String name = node.getNodeName();
String attrValue = "", attrName = "";
System.out.println("This is element! name is:" + name);
if (node.hasAttributes()){
NamedNodeMap attrlist = node.getAttributes();
for (int j=0; j<attrlist.getLength(); j++){
Node attrNode = attrlist.item(j);
attrName = attrNode.getNodeName();
attrValue = attrNode.getNodeValue();
System.out.println("this element attr is:"+
attrValue + ";attrname is :" + attrName);
}
}
//如有子节点,递归调用getElement()
if (node.hasChildNodes()){
getElement(node.getChildNodes());
}
}
/*Text类型节点没有子节点,节点名为#text,节点的值为XML文档中的元素值*/
if (nodetype == Node.TEXT_NODE){
//该节点的name是“#text”
String txtName = node.getNodeName();
//取出Text类型节点的值
Node thisparent = node.getParentNode();
Node txtNode = thisparent.getChildNodes().item(0);
String txtValue = txtNode.getNodeValue();
if (txtValue.trim().length() > 0){
System.out.println("txtName=" + txtName +
"; txtValue = " + txtValue.trim());
}
}
}
}
}
<?xml version="1.0" encoding='utf-8'?>
<completedRequest currentTime='' startDay='2009-12-23'
endDay='2009-12-23'>
<building buildingName='1'>
<floor floorName='1'>
<line lineName='N1'>
<completedInfo seatNo='11' responseTime='11:25'
RepairTime='11:23'>
<completedDetail requestTime='11:23'
requestStaffNo='H0050' requestStaffName='张三' responseTime='11:25'
responseStaffNo='J332' responseStaffName='李四' completeTime='11:50'
completeStaffNo='Y6500' completeStaffName='梁五'>
</completedDetail>
</completedInfo>
<completedInfo seatNo='12' responseTime='11:25'
RepairTime='11:23'>
<completedDetail requestTime='11:23'
requestStaffNo='H00501' requestStaffName='张三1' responseTime='11:25'
responseStaffNo='J3321' responseStaffName='李四1' completeTime='11:50'
completeStaffNo='Y65001' completeStaffName='梁五1'>
</completedDetail>
</completedInfo>
</line>
</floor>
</building>
<building buildingName='1'>
<floor floorName='1'>
<line lineName='N1'>
<completedInfo seatNo='11' responseTime='11:25'
RepairTime='11:23'>
<completedDetail requestTime='11:23'
requestStaffNo='H0050' requestStaffName='张三' responseTime='11:25'
responseStaffNo='J332' responseStaffName='李四' completeTime='11:50'
completeStaffNo='Y6500' completeStaffName='梁五'>
</completedDetail>
</completedInfo>
<completedInfo seatNo='12' responseTime='11:25'
RepairTime='11:23'>
<completedDetail requestTime='11:23'
requestStaffNo='H00501' requestStaffName='张三1' responseTime='11:25'
responseStaffNo='J3321' responseStaffName='李四1' completeTime='11:50'
completeStaffNo='Y65001' completeStaffName='梁五1'>
</completedDetail>
</completedInfo>
</line>
</floor>
</building>
<building buildingName='1'>
<floor floorName='1'>
<line lineName='N1'>
<completedInfo seatNo='11' responseTime='11:25'
RepairTime='11:23'>
<completedDetail requestTime='11:23'
requestStaffNo='H0050' requestStaffName='张三' responseTime='11:25'
responseStaffNo='J332' responseStaffName='李四' completeTime='11:50'
completeStaffNo='Y6500' completeStaffName='梁五'>
</completedDetail>
</completedInfo>
<completedInfo seatNo='12' responseTime='11:25'
RepairTime='11:23'>
<completedDetail requestTime='11:23'
requestStaffNo='H00501' requestStaffName='张三1' responseTime='11:25'
responseStaffNo='J3321' responseStaffName='李四1' completeTime='11:50'
completeStaffNo='Y65001' completeStaffName='梁五1'>
</completedDetail>
</completedInfo>
</line>
</floor>
</building>
</completedRequest>
结果:
=============================================================
根节点的名字是:completedRequest
子节点的长度是:7
This is element! name is:building
this element attr is:1;attrname is :buildingName
This is element! name is:floor
this element attr is:1;attrname is :floorName
This is element! name is:line
this element attr is:N1;attrname is :lineName
This is element! name is:completedInfo
this element attr is:11:23;attrname is :RepairTime
this element attr is:11:25;attrname is :responseTime
this element attr is:11;attrname is :seatNo
This is element! name is:completedDetail
this element attr is:梁五;attrname is :completeStaffName
this element attr is:Y6500;attrname is :completeStaffNo
this element attr is:11:50;attrname is :completeTime
this element attr is:张三;attrname is :requestStaffName
this element attr is:H0050;attrname is :requestStaffNo
this element attr is:11:23;attrname is :requestTime
this element attr is:李四;attrname is :responseStaffName
this element attr is:J332;attrname is :responseStaffNo
this element attr is:11:25;attrname is :responseTime
This is element! name is:completedInfo
this element attr is:11:23;attrname is :RepairTime
this element attr is:11:25;attrname is :responseTime
this element attr is:12;attrname is :seatNo
This is element! name is:completedDetail
this element attr is:梁五1;attrname is :completeStaffName
this element attr is:Y65001;attrname is :completeStaffNo
this element attr is:11:50;attrname is :completeTime
this element attr is:张三1;attrname is :requestStaffName
this element attr is:H00501;attrname is :requestStaffNo
this element attr is:11:23;attrname is :requestTime
this element attr is:李四1;attrname is :responseStaffName
this element attr is:J3321;attrname is :responseStaffNo
this element attr is:11:25;attrname is :responseTime
This is element! name is:building
this element attr is:1;attrname is :buildingName
This is element! name is:floor
this element attr is:1;attrname is :floorName
This is element! name is:line
this element attr is:N1;attrname is :lineName
This is element! name is:completedInfo
this element attr is:11:23;attrname is :RepairTime
this element attr is:11:25;attrname is :responseTime
this element attr is:11;attrname is :seatNo
This is element! name is:completedDetail
this element attr is:梁五;attrname is :completeStaffName
this element attr is:Y6500;attrname is :completeStaffNo
this element attr is:11:50;attrname is :completeTime
this element attr is:张三;attrname is :requestStaffName
this element attr is:H0050;attrname is :requestStaffNo
this element attr is:11:23;attrname is :requestTime
this element attr is:李四;attrname is :responseStaffName
this element attr is:J332;attrname is :responseStaffNo
this element attr is:11:25;attrname is :responseTime
This is element! name is:completedInfo
this element attr is:11:23;attrname is :RepairTime
this element attr is:11:25;attrname is :responseTime
this element attr is:12;attrname is :seatNo
This is element! name is:completedDetail
this element attr is:梁五1;attrname is :completeStaffName
this element attr is:Y65001;attrname is :completeStaffNo
this element attr is:11:50;attrname is :completeTime
this element attr is:张三1;attrname is :requestStaffName
this element attr is:H00501;attrname is :requestStaffNo
this element attr is:11:23;attrname is :requestTime
this element attr is:李四1;attrname is :responseStaffName
this element attr is:J3321;attrname is :responseStaffNo
this element attr is:11:25;attrname is :responseTime
This is element! name is:building
this element attr is:1;attrname is :buildingName
This is element! name is:floor
this element attr is:1;attrname is :floorName
This is element! name is:line
this element attr is:N1;attrname is :lineName
This is element! name is:completedInfo
this element attr is:11:23;attrname is :RepairTime
this element attr is:11:25;attrname is :responseTime
this element attr is:11;attrname is :seatNo
This is element! name is:completedDetail
this element attr is:梁五;attrname is :completeStaffName
this element attr is:Y6500;attrname is :completeStaffNo
this element attr is:11:50;attrname is :completeTime
this element attr is:张三;attrname is :requestStaffName
this element attr is:H0050;attrname is :requestStaffNo
this element attr is:11:23;attrname is :requestTime
this element attr is:李四;attrname is :responseStaffName
this element attr is:J332;attrname is :responseStaffNo
this element attr is:11:25;attrname is :responseTime
This is element! name is:completedInfo
this element attr is:11:23;attrname is :RepairTime
this element attr is:11:25;attrname is :responseTime
this element attr is:12;attrname is :seatNo
This is element! name is:completedDetail
this element attr is:梁五1;attrname is :completeStaffName
this element attr is:Y65001;attrname is :completeStaffNo
this element attr is:11:50;attrname is :completeTime
this element attr is:张三1;attrname is :requestStaffName
this element attr is:H00501;attrname is :requestStaffNo
this element attr is:11:23;attrname is :requestTime
this element attr is:李四1;attrname is :responseStaffName
this element attr is:J3321;attrname is :responseStaffNo
this element attr is:11:25;attrname is :responseTime
===============================================================
- 大小: 13.1 KB
分享到:
相关推荐
linux基础进阶笔记,配套视频:https://www.bilibili.com/list/474327672?sid=4493093&spm_id_from=333.999.0.0&desc=1
IMG20241115211541.jpg
GEE训练教程——Landsat5、8和Sentinel-2、DEM和各2哦想指数下载
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过严格测试运行成功才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。
基于springboot家政预约平台源码数据库文档.zip
Ucharts添加stack和折线图line的混合图
基于springboot员工在线餐饮管理系统源码数据库文档.zip
新能源汽车进出口数据 1、时间跨度:2018-2020年 2、指标说明:包含如下指标的进出口数据:混合动力客车(10座及以上)、纯电动客车(10座及以上)、非插电式混合动力乘用车、插电式混合动力乘用车、纯电动乘用车 二、新能源汽车进出口月销售数据(分地区、分类型、分 级别) 1、数据来源:见资料内说明 2、时间跨度:2014年1月-2021年5月 4、指标说明: 包含如下指标 2015年1月-2021年5月新能源乘用车终端月度销量(分类型)部分内容如下: 新能源乘用车(单月值、累计值 )、插电式混合动力 月度销量合计(狭义乘用车轿车、SUV、MPV、交叉型乘用车); 月度销量同比增速(狭义乘用车轿车、SUV、MPV、交叉型乘用车); 累计销量合计(狭义乘用车轿车、SUV、IPV、交叉型乘用车); 累计销量同比增速(狭义乘用车轿车、SUV、MPV、交叉型乘用车); 累计结构变化(狭义乘用车轿车、SUV、IPV、交叉型乘用车); 2015年1月-2021年5月新能源乘用车终端月度销量(分地区)内容如下: 更多见资源内
中心主题-241121215200.pdf
内容概要:本文档提供了多个蓝奏云下载链接及其对应解压密码,帮助用户快速获取所需文件。 适合人群:需要从蓝奏云下载文件的互联网用户。 使用场景及目标:方便地记录并分享蓝奏云上文件的下载地址和密码,提高下载效率。 阅读建议:直接查看并使用提供的链接和密码即可。若遇到失效情况,请尝试联系上传者确认更新后的链接。
基于Java web 实现的仓库管理系统源码,适用于初学者了解Java web的开发过程以及仓库管理系统的实现。
资源名称:Python-文件重命名-自定义添加文字-重命名 类型:windows—exe可执行工具 环境:Windows10或以上系统 功能: 1、点击按钮 "源原文"【浏览】表示:选择重命名的文件夹 2、点击按钮 "保存文件夹"【浏览】表示:保存的路径(为了方便可选择保存在 源文件中 ) 3、功能①:在【头部】添加自定义文字 4、功能②:在【尾部】添加自定义文字 5、功能③:输入源字符 ;输入替换字符 可以将源文件中的字符替换自定义的 6、功能④:自动加上编号_1 _2 _3 优点: 1、非常快的速度! 2、已打包—双击即用!无需安装! 3、自带GUI界面方便使用!
JDK8安装包
配合作者 一同使用 作者地址没有次下载路径 https://blog.csdn.net/weixin_52372189/article/details/127471149?fromshare=blogdetail&sharetype=blogdetail&sharerId=127471149&sharerefer=PC&sharesource=weixin_45375332&sharefrom=from_link
GEE训练教程
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过严格测试运行成功才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。
基于springboot交通感知与车路协同系统源码数据库文档.zip
基于springboot+vue 雅妮电影票购买系统源码数据库文档.zip
为了更好地理解 HTML5 的拖放功能,我们设计了一个简单有趣的示例:将水果从水果区拖放到购物笼中,实时更新数量和价格,并在所有水果被成功放置后,播放音效并显示提示。
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过严格测试运行成功才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。