




已阅读5页,还剩6页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
XML基础教程 课后习题习题一 1答:HTML是用来编写Web页的语言、不允许用户自定义标记,HTML体现数据的显示格式。XML描述数据的组织结构、可自定义标记,其标记名称是对标记所包含的数据内容含义的抽象,而不是数据的显示格式。2答:使用UTF-8保存5 答:(1)不可以,(2)可以,(3)不可以6答:Show.css:time display:block;font-size:18pt;font-weight:boldhour display:line;font-size:16pt;font-style:italicmimute display:line;font-size:9pt;font-weight:bold习题二1答:(1)使用ANSI编码。(2)可以。(3)不合理。2答:不相同。3答:(1)和(2)。4答:A3.xml。5答:“root”标记包含的文本内容都是空白字符。 “a1”标记包含的文本内容:。“a2”标记包含的文本内容: 子曰有朋自远方来,不亦乐乎。习题三1答:一个规范的XML文件如果和某个DTD文件相关联,并遵守该DTD文件规定的约束条件,就称之为有效的XML文件。2答:DTD文件的编码必须和其约束的XML文件的编码相一致。3答:无关。4答:(1) 使用SYSTEM文档类型声明的格式:(2) 使用PUBLIC文档类型声明的格式:5答:一定。6答:(1)约束标记“张三”必须有“学号”属性 (2)约束标记“张三”必须有“学号”属性,而且学号的属性值是固定的220123。 (3)约束标记“张三”可以有也可以没有“学号”属性。7答:ID类型的属性的属性值具有互斥性,即所有ID类型的属性的属性值必须互不相同。8答:不合理。9答:(1)、(3)和(4)。10答,B.xml不是有效的。将B.xml修改为有效:B.xml 张三 优秀 李四 良好 习题四1.答:一个返回子孙节点,而另一个只返回子节点:(1)getElementsByTagName(String name):返回一个NodeList对象,该对象由当前节点的Element类型子孙节点组成,这些子孙节点的名字由参数name指定(2)getChildNodes():节点调用该方法返回一个由当前节点的所有子节点组成的NodeList对象。2答:分别是DocumentType类型和Element类型。3 JAXPXiti5.javaimport org.w3c.dom.*; import javax.xml.parsers.*;import java.io.*;public class JAXPXiti5 public static void main(String args) OutResult outContent=new OutResult(); try DocumentBuilderFactory factory= DocumentBuilderFactory. newInstance(); DocumentBuilder builder= factory. newDocumentBuilder(); Document document= builder. parse(new File(a.xml) ; Element root=document.getDocumentElement() ; String rootName=root.getNodeName(); NodeList nodeList=root.getChildNodes(); outContent.output(nodeList); System.out.println(数学平均成绩+outContent.mathAver); System.out.println(物理平均成绩+outContent.physicsAver); catch(Exception e) System.out.println(e); class OutResult double sumAllMath,sumAllPhysics,mathAver,physicsAver; int mathNumber=0, physicsNumber=0; public void output(NodeList nodeList) int size=nodeList.getLength(); for(int k=0;ksize;k+) Node node=nodeList.item(k); if(node.getNodeType()=Node.TEXT_NODE) Text textNode=(Text)node; String content=textNode.getWholeText(); System.out.print(content); Element parent=(Element)textNode.getParentNode(); if(parent.getNodeName().equals(数学) mathNumber+; sumAllMath=sumAllMath+Double.parseDouble(content.trim(); if(parent.getNodeName().equals(物理) physicsNumber+; sumAllPhysics=sumAllPhysics+Double.parseDouble(content.trim(); if(node.getNodeType()=Node.ELEMENT_NODE) Element elementNode=(Element)node; String name=elementNode.getNodeName(); System.out.print(name); NodeList nodes=elementNode.getChildNodes(); output(nodes); mathAver=sumAllMath/mathNumber; physicsAver=sumAllPhysics/physicsNumber; 习题五:DOM解析器的核心是在内存中建立和XML文件相对应的树形结构数据,如果XML文件较大,相应的Document对象就要占用较多的内存空间;SAX解析器不在内存中建立和XML文件相对应的树形结构数据,和DOM解析器相比,SAX解析器占有的内存少。1答:不报告。(这个答案不知道)2答:uri和localName的取值是空字符组成的串,即uri=,localName=,参数qName取值是标记名称,atts是标记的所有属性的集合。3答:当忽略标记之间形成的空白区域时。4.答:Xiti7.javaimport org.w3c.dom.*; import javax.xml.parsers.*;import java.io.*;public class Xiti7 public static void main(String args) OutTotal outWeight=new OutTotal(); try DocumentBuilderFactory factory=DocumentBuilderFactory. newInstance(); DocumentBuilder builder= factory. newDocumentBuilder(); Document document= builder.parse(new File(a.xml) ; Element root=document.getDocumentElement() ; NodeList nodeList=root.getChildNodes(); outWeight.output(nodeList); System.out.println(总重量:+outWeight.sum); catch(Exception e) System.out.println(e); class OutTotal double sum; public void output(NodeList nodeList) int size=nodeList.getLength(); for(int k=0;ksize;k+) Node node=nodeList.item(k); if(node.getNodeType()=Node.TEXT_NODE) Text textNode=(Text)node; String content=textNode.getWholeText(); System.out.print(content); Element parent=(Element)textNode.getParentNode(); if(parent.getNodeName().equals(重量) sum=sum+Double.parseDouble(content.trim(); if(node.getNodeType()=Node.ELEMENT_NODE) Element elementNode=(Element)node; String name=elementNode.getNodeName(); System.out.print(name); NodeList nodes=elementNode.getChildNodes(); output(nodes); 习题六1答:Element类型节点对应XML中的标记。2答:(1)按顺序对应着第一个名字是“教室”的标记中的第一个名字是“桌子”标记的“价格”标记。(2) 对应着具有属性number,属性值是02、名字为“桌子”的标记,而且这些桌子标记有子标记“价格”,而且价格包含的文本是数字且值大于等于360。(3)对应“教室”的属性。(4)number属性所在的“教室”节点。(5)109(6)396,267(7)对应着“教室”标记,具教室标记有孙标记“价格”,且“价格”标记含有的数字文本的值大于390,即是第2个“教室”标记。(8)对应着价格标记,且“价格”标记含有的数字文本的值大于396。3答:(1) 【结果1】: A1001定单的总额:(2) 【结果2】: 40.0(3) 【结果3】: 4(4) 【结果4】:1004答:Xiti8.javaimport javax.xml.xpath.*;import org.xml.sax.*;import org.w3c.dom.*; public class Xiti8 public static void main(String args) try XPathFactory xPathFactory=XPathFactory.newInstance(); XPath xPath=xPathFactory.newXPath(); InputSource source=new InputSource(Xiti8.xml); String countPath=count(/货品列表/货品/重量); String sumPath=sum(/货品列表/货品/重量); Double sum=(Double)xPath.evaluate(sumPath,source,XPathConstants.NUMBER); double total=sum.doubleValue(); System.out.println(总重量:+total); catch(Exception exp) System.out.println(exp); 5答:Student.xml 张三 男 29 李张林 男 18 张李号 女 25 Aplication.javapublic class Application public static void main(String args) new StudentConditionWindow(); StudentConditionWindow.javaimport java.awt.*;import javax.swing.*;public class StudentConditionWindow extends JFrame JTextField inputName,inputNumber; JTextArea showResult; JButton button; Box baseBox ,boxV1,boxV2; StudentConditionWindow() inputName=new JTextField(10); inputNumber=new JTextField(10); boxV1=Box.createVerticalBox(); boxV1.add(new Label(姓名中包含:); boxV1.add(new Label(学号中包含); boxV2=Box.createVerticalBox(); boxV2.add(inputName); boxV2.add(inputNumber); baseBox=Box.createHorizontalBox(); baseBox.add(boxV1); baseBox.add(boxV2); JPanel west=new JPanel(); west.add(baseBox); button=new JButton(确定); west.add(button); add(west,BorderLayout.WEST); showResult=new JTextArea(10,10); showResult.setFont(new Font(宋体,Font.PLAIN,12); add(new JScrollPane(showResult),BorderLayout.CENTER); FindStudentByXPath findStudent; /负责使用XPath查询学生的对象 findStudent= new FindStudentByXPath(inputName,inputNumber,showResult,student.xml); button.addActionListener(findStudent); setBounds(10,10,900,300); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); FindStudentByXPath.javaimport javax.xml.xpath.*;import org.w3c.dom.*; import javax.swing.*;import java.awt.event.*;import org.xml.sax.*;public class FindStudentByXPath implements ActionListener XPathFactory xPathFactory; XPath xPath; String positionPath; InputSource source; JTextField inputName,inputNumber; JTextArea showResult; FindStudentByXPath(JTextField inputName,JTextField inputNumber,JTextArea showResult,String fileName) this.inputName=inputName; this.inputNumber=inputNumber; this.showResult=showResult; xPathFactory=XPathFactory.newInstance(); xPath=xPathFactory.newXPath(); source=new InputSource(fileName); this.positionPath=positionPath; public void actionPerformed(ActionEvent e) showResult.setText(null); String studentName=inputName.getText().trim(); String studentNumber=inputNumber.getText().trim(); String predicates=contains(姓名,+studentName+) and + contains(学号,+studentNumber+); String path=/学生列表/学生+predicates+/*; /XPath路径表达式 try NodeList nodelist= (NodeList)xPath.evaluate(path,source,XPathConstants.NODESET); int size=nodelist.getLength(); for(int k=0;ksize;k+) Node node=nodelist.item(k); String name=node.getNodeName(); showResult.append( +name+:); String content=node.getTextContent(); showResult.append(content+n); if(name.startsWith(年龄) showResul
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024二年级语文下册 第2单元 6 千人糕说课稿 新人教版
- 2024-2025学年八年级物理下册 第九章 压强 第2节 液体的压强说课稿 (新版)新人教版
- 第六课 初定目标话追求说课稿-2025-2026学年初中心理健康北师大版河南专版九年级全一册-北师大版河南专版
- 3 呵护人类共有的家园教学设计-2025-2026学年高中思想政治人教版选修6公民道德与伦理常识-人教版
- Unit 3 Amazing animals Letters and sounds 第3课时(教学设计)-2024-2025学年人教PEP版(2024)英语三年级上册
- 2025年中考数学总复习《三角形的中位线》专项检测卷(附答案)
- 2025年中考地理试题分类汇编:陆地与海洋、天气与气候(第1期)原卷版
- 2025年2月高级西式面点师模拟练习题(含答案)
- 2025年全国煤矿特种作业人员采煤机司机考试题库(含答案)
- 2025年北京高考地理试题(解析版)
- 电子学会2024年3月青少年软件编程Python等级考试试卷二级真题(含答案)
- 内蒙古自治区赤峰市红山区2023-2024学年八年级下学期7月期末历史试题
- 四圣心源方剂
- 弱电安全技术交底【范本模板】
- 生物化学之蛋白质化学(唐炳华)
- 产品保修卡模板
- 2024年福建浦开集团有限公司招聘笔试参考题库附带答案详解
- 文旅集团招聘试题
- 二次装修管理培训课件
- 小学语文教研组秋季工作计划
- 五年级人自然社会教案
评论
0/150
提交评论