版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
淮海工学院计算机工程学院实验报告书课程名:《XML开发技术》题目:DOM解析学号:姓名:评语:评语:成绩:指导教师:批阅时间:年月日《XML开发技术》实验报告一、实验目的与要求DOM(DocumentObjectModel,文档对象模型)是W3C制定的一套规范标准,即规定了解析文件的接口DOM规范的核心是树模型。对于解析XML文件的解析器,解析器通过读入XML文件在内存中建立一个树,也就是说XML文件的标记、标记的文本内容、实体等都会和内存中树的某个节点相对应。1.掌握DO解析器的工作原理;2.掌握节点的类型;3.熟练掌握Element、Text、Document等节点的使用。二、实验内容或题目使用DOM解析器读取存储CD信息的XML文件,练习Element、Text、Document等节点的使用。三、实验步骤与源程序1.使用以前创建的XML文件,文件中包含多条CD信息、图书信息等。2.使用DOM,编写JAVA程序,读取XML文件中的数据,并显示到控制台上。使用两种方法实现该功能:1)使用getElementsByTagName()方法2)使用getChildNodes()方法3.属性值的读取使用两种 1)使用getAttribute方法 2)使用ATTR节点//getChildNode.javaimportjava.io.File;importjavax.xml.parsers.DocumentBuilder;importjavax.xml.parsers.DocumentBuilderFactory;importorg.w3c.dom.Document;importorg.w3c.dom.Element;importorg.w3c.dom.Node;importorg.w3c.dom.NodeList;publicclassgetChildNode{ publicstaticvoidmain(Stringargs[]) {try{ DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance(); DocumentBuilderbuilder=factory.newDocumentBuilder(); Documentdocument=builder.parse(newFile("Student.xml")); Elementroot=document.getDocumentElement(); Stringrootname=root.getNodeName(); System.out.println("XML文件根节点的名字:"+rootname); NodeListnodeList=root.getChildNodes(); intsize=nodeList.getLength(); for(intk=0;k<size;k++) {Nodenode=nodeList.item(k); if(node.getNodeType()==Node.ELEMENT_NODE) {ElementelementNode=(Element)node; Stringname=elementNode.getNodeName(); Stringid=elementNode.getAttribute("id"); Stringsex=elementNode.getAttribute("sex"); Stringcontent=elementNode.getTextContent(); System.out.print(name); System.out.print("id="+id); System.out.println("sex="+sex); System.out.println(content);}}}catch(Exceptione){System.out.println(e);}}}//getElement.javaimportjava.io.File;importjavax.xml.parsers.DocumentBuilder;importjavax.xml.parsers.DocumentBuilderFactory;importorg.w3c.dom.Attr;importorg.w3c.dom.Document;importorg.w3c.dom.Element;importorg.w3c.dom.NamedNodeMap;importorg.w3c.dom.Node;importorg.w3c.dom.NodeList;publicclassStudent{publicstaticvoidmain(Stringargs[]){DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance(); DocumentBuilderbulBuilder; try{ bulBuilder=factory.newDocumentBuilder(); Documentdocument=bulBuilder.parse(newFile("Student.xml")); Elementroot=document.getDocumentElement(); Stringrootname=root.getNodeName(); System.out.println(rootname); NodeListnodeList=document.getElementsByTagName("学生"); intsize=nodeList.getLength(); for(intk=0;k<size;k++) {Nodenode=nodeList.item(k); Stringname=node.getNodeName(); NamedNodeMapmap=node.getAttributes(); Stringcontent=node.getTextContent(); System.out.print(name); for(inti=0;i<map.getLength();i++) {AttrattrNode=(Attr)map.item(i); StringattName=attrNode.getName(); StringattValue=attrNode.getValue(); System.out.print(""+attName+"="+attValue+"");} System.out.print(content);} }catch(Exceptione){ e.printStackTrace();}}4.阅读并调试如下代码,要求读懂程序。
<html>
<head>
<scripttype="text/javascript"src="/example/xdom/loadxmldoc.js"></script>
</head>
<body>
<scripttype="text/javascript">
xmlDoc=loadXMLDoc("/example/xdom/books.xml");/*加载该xml文件*/document.write(xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue);/*输出第一个title标记的第一个孩子节点的值*/
document.write("<br/>");/*输出换行*/
document.write(xmlDoc.getElementsByTagName("author")[0].childNodes[0].nodeValue);/*输出author标记的第一个孩子节点的值*/document.write("<br/>");
document.write(xmlDoc.getElementsByTagName("year")[0].childNodes[0].nodeValue
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026四川广元朝天公安招聘警务辅助人员20人笔试参考试题及答案详解
- 中国民生银行广州分行2026届校园招聘笔试参考试题及答案详解
- 2026江苏南京大学YJ202605941电子科学与工程学院特任副研究员招聘1人笔试参考试题及答案详解
- 2026浙江衢州市特种设备检验检测研究院招聘编外人员第一批第二次5人笔试参考题库及答案详解
- 2026江西南昌华云气象广告有限公司劳务派遣岗位(平面设计师)招聘1人笔试备考试题及答案详解
- 内蒙古兴安盟2026届中考语文模拟试题含解析
- 2026湖南永顺经开区建设投资开发有限责任公司公开招聘5人笔试参考试题及答案详解
- 2026年杭州市萧山区街道办人员招聘考试备考试题及答案解析
- 2026天津市肿瘤医院教学处教学辅助岗位招聘笔试备考试题及答案详解
- 2025年南京市建邺区幼儿园教师招聘考试试题及答案解析
- 四川省成都市成华区片区联考2025-2026学年八年级(上学期)期中英语试卷(含解析)
- 2025年顺丰快递员劳动合同模板
- 2026年中考语文专题复习:标点符号 讲义
- 常见病小儿推拿培训
- 政务颁奖礼仪培训
- 疝气手术护理宣教
- 行文格式规范培训
- 肝癌破裂出血的护理
- 持续时间模型的估计与检验
- 螺旋改直升气柜施工方案
- 陶瓷茶具基本知识培训课件
评论
0/150
提交评论