XML基础教程课后习题解答Word文件下载.docx
- 文档编号:19786431
- 上传时间:2023-01-10
- 格式:DOCX
- 页数:15
- 大小:19.91KB
XML基础教程课后习题解答Word文件下载.docx
《XML基础教程课后习题解答Word文件下载.docx》由会员分享,可在线阅读,更多相关《XML基础教程课后习题解答Word文件下载.docx(15页珍藏版)》请在冰豆网上搜索。
CCTV5>
。
“a2”标记包含的文本内容:
子曰"
有朋自远方来,不亦乐乎"
习题三1.答:
一个规范的XML文件如果和某个DTD文件相关联,并遵守该DTD文件规定的约束条件,就称之为有效的XML文件。
DTD文件的编码必须和其约束的XML文件的编码相一致。
无关。
(1)使用SYSTEM文档类型声明的格式:
DOCTYPE根标记的名称SYSTEM"
DTD文件的URI"
>
(2)使用PUBLIC文档类型声明的格式:
!
DOCTYPE根标记的名称PUBLIC"
正式公用标识符"
"
一定。
(1)约束标记“张三”必须有“学号”属性
(2)约束标记“张三”必须有“学号”属性,而且学号的属性值是固定的220123。
(3)约束标记“张三”可以有也可以没有“学号”属性。
7.答:
ID类型的属性的属性值具有互斥性,即所有ID类型的属性的属性值必须互不相同。
8.答:
不合理。
9.答:
(1)、(3)和(4)。
10.答,B.xml不是有效的。
将B.xml修改为有效:
B.xml
?
xmlversion="
1.0"
encoding="
UTF-8"
?
DOCTYPE成绩单SYSTEM"
A.dtd"
成绩单>
<
学生>
姓名>
张三<
/姓名>
成绩>
优秀<
/成绩>
/学生>
李四<
良好<
/成绩单>
习题四
1.答:
一个返回子孙节点,而另一个只返回子节点:
(1)getElementsByTagName(Stringname):
返回一个NodeList对象,该对象由当前节点的Element类型子孙节点组成,这些子孙节点的名字由参数name指定
(2)getChildNodes():
节点调用该方法返回一个由当前节点的所有子节点组成的NodeList对象。
分别是DocumentType类型和Element类型。
3.
JAXPXiti5.java
importorg.w3c.dom.*;
importjavax.xml.parsers.*;
importjava.io.*;
publicclassJAXPXiti5
{
publicstaticvoidmain(Stringargs[])
{
OutResultoutContent=newOutResult();
try{
DocumentBuilderFactoryfactory=
DocumentBuilderFactory.newInstance();
DocumentBuilderbuilder=factory.newDocumentBuilder();
Documentdocument=builder.parse(newFile("
a.xml"
));
Elementroot=document.getDocumentElement();
StringrootName=root.getNodeName();
NodeListnodeList=root.getChildNodes();
outContent.output(nodeList);
System.out.println("
数学平均成绩"
+outContent.mathAver);
物理平均成绩"
+outContent.physicsAver);
}
catch(Exceptione)
{
System.out.println(e);
}
classOutResult
{
doublesumAllMath,sumAllPhysics,mathAver,physicsAver;
intmathNumber=0,physicsNumber=0;
publicvoidoutput(NodeListnodeList)
intsize=nodeList.getLength();
for(intk=0;
k<
size;
k++)
Nodenode=nodeList.item(k);
if(node.getNodeType()==Node.TEXT_NODE)
TexttextNode=(Text)node;
Stringcontent=textNode.getWholeText();
System.out.print(content);
Elementparent=(Element)textNode.getParentNode();
if(parent.getNodeName().equals("
数学"
))
mathNumber++;
sumAllMath=sumAllMath+Double.parseDouble(content.trim());
物理"
physicsNumber++;
sumAllPhysics=sumAllPhysics+Double.parseDouble(content.trim());
if(node.getNodeType()==Node.ELEMENT_NODE)
ElementelementNode=(Element)node;
Stringname=elementNode.getNodeName();
System.out.print(name);
NodeListnodes=elementNode.getChildNodes();
output(nodes);
mathAver=sumAllMath/mathNumber;
physicsAver=sumAllPhysics/physicsNumber;
习题五:
DOM解析器的核心是在内存中建立和XML文件相对应的树形结构数据,如果XML文件较大,相应的Document对象就要占用较多的内存空间;
SAX解析器不在内存中建立和XML文件相对应的树形结构数据,和DOM解析器相比,SAX解析器占有的内存少。
不报告。
(这个答案不知道)
uri和localName的取值是空字符组成的串,即uri="
"
,localName="
,参数qName取值是标记名称,atts是标记的所有属性的集合。
当忽略标记之间形成的空白区域时。
4.答:
Xiti7.java
publicclassXiti7
OutTotaloutWeight=newOutTotal();
DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
Documentdocument=builder.parse(newFile("
outWeight.output(nodeList);
总重量:
+outWeight.sum);
catch(Exceptione){
classOutTotal
doublesum;
重量"
))
sum=sum+Double.parseDouble(content.trim());
}
习题六1.答:
Element类型节点对应XML中的标记。
(1)按顺序对应着第一个名字是“教室”的标记中的第一个名字是“桌子”标记的“价格”标记。
(2)对应着具有属性number,属性值是02、名字为“桌子”的标记,而且这些桌子标记有子标记“价格”,而且价格包含的文本是数字且值大于等于360。
(3)对应“教室”的属性。
(4)number属性所在的“教室”节点。
(5)109
(6)396,267
(7)对应着“教室”标记,具教室标记有孙标记“价格”,且“价格”标记含有的数字文本的值大于390,即是第2个“教室”标记。
(8)对应着价格标记,且“价格”标记含有的数字文本的值大于396。
(1)【结果1】:
A1001定单的总额:
(2)【结果2】:
40.0
(3)【结果3】:
4
(4)【结果4】:
100
Xiti8.java
importjavax.xml.xpath.*;
importorg.xml.sax.*;
publicclassXiti8{
publicstaticvoidmain(Stringargs[]){
try{XPathFactoryxPathFactory=XPathFactory.newInstance();
XPathxPath=xPathFactory.newXPath();
InputSourcesource=newInputSource("
Xiti8.xml"
);
StringcountPath="
count(/货品列表/货品/重量)"
;
StringsumPath="
sum(/货品列表/货品/重量)"
Doublesum=(Double)xPath.evaluate(sumPath,source,XPathConstants.NUMBER);
doubletotal=sum.doubleValue();
总重量:
+total);
catch(Exceptionexp){
System.out.println(exp);
Student.xml
学生列表>
学生学号='
72349876'
张三<
性别>
男<
/性别>
年龄>
29<
/年龄>
12345678'
李张林<
18<
000'
张李号<
女<
25<
/学生列表>
Aplication.java
publicclassApplication{
newStudentConditionWindow();
StudentConditionWindow.java
importjava.awt.*;
importjavax.swing.*;
publicclassStudentConditionWindowextendsJFrame{
JTextFieldinputName,inputNumber;
JTextAreashowResult;
JButtonbutton;
BoxbaseBox,boxV1,boxV2;
StudentConditionWindow(){
inputName=newJTextField(10);
inputNumber=newJTextField(10);
boxV1=Box.createVerticalBox();
boxV1.add(newLabel("
姓名中包含:
));
学号中包含"
boxV2=Box.createVerticalBox();
boxV2.add(inputName);
boxV2.add(inputNumber);
baseBox=Box.createHorizontalBox();
baseBox.add(boxV1);
baseBox.add(boxV2);
JPanelwest=newJPanel();
west.add(baseBox);
button=newJButton("
确定"
west.add(button);
add(west,BorderLayout.WEST);
showResult=newJTextArea(10,10);
showResult.setFont(newFont("
宋体"
Font.PLAIN,12));
add(newJScrollPane(showResult),BorderLayout.CENTER);
FindStudentByXPathfindStudent;
//负责使用XPath查询学生的对象
findStudent=
newFindStudentByXPath(inputName,inputNumber,showResult,"
student.xml"
button.addActionListener(findStudent);
setBounds(10,10,900,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FindStudentByXPath.java
importjava.awt.event.*;
publicclassFindStudentByXPathimplementsActionListener{
XPathFactoryxPathFactory;
XPathxPath;
StringpositionPath;
InputSourcesource;
FindStudentByXPath(JTextFieldinputName,JTextFieldinputNumber,JTextAreashowResult,StringfileName){
this.inputName=inputName;
this.inputNumber=inputNumber;
this.showResult=showResult;
xPathFactory=XPathFactory.newInstance();
xPath=xPathFactory.newXPath();
source=newInputSource(fileName);
this.positionPath=positionPath;
publicvoidactionPerformed(ActionEvente){
showResult.setText(null);
StringstudentName=inputName.getText().trim();
StringstudentNumber=inputNumber.getText().trim();
Stringpredicates="
[contains(姓名,'
+studentName+"
'
)and"
+
contains(@学号,'
+studentNumber+"
)]"
Stringpath="
/学生列表/学生"
+predicates+"
/*"
//XPath路径表达式
try{
NodeListnodelist=
(NodeList)xPath.evaluate(path,source,XPathConstants.NODESET);
intsize=nodelist.getLength();
k++){
Nodenode=nodelist.item(k);
Stringname=node.getNodeName();
showResult.append("
+name+"
:
Stringcontent=node.getTextContent();
showResult.append(content+"
\n"
if(name.startsWith("
年龄"
--------------------------\n"
异常:
+exp);
习题七
参考例子2即可完成。
2.答参见例子3即可完成。
习题八1.答:
使用操作指令:
xml-stylesheethref="
样式表的URI"
type="
text/css"
student
color:
red;
假设XML文件中有三个标记的名称都是“name”,“ID”属性分别是a001,a002,a003。
CSS文件如下:
namea001
border-style:
double;
width=260;
height=120
namea002
dotted;
width=150;
height=60;
font-size:
10pt;
namea003
ridge;
width=90;
height=30;
修改XML文件。
math
12pt;
font-style:
italic;
background-color:
rgb(227,228,229);
chemistry
text-decoration:
underline;
cyan;
low
8pt;
vertical-align:
bottom;
习题九
1.答:
不可以
2.答:
3.答:
student.xml
雇员列表>
雇员>
月薪>
1235.89<
/月薪>
参加工作时间>
2000-12-12<
/参加工作时间>
/雇员>
李四<
3234.82<
2001-10-28<
/雇员列表>
employee.xsd
xsd:
schemaxmlns:
xsd="
http:
//www.w3.org/2001/XMLSchema"
elementname="
雇员列表"
>
complexType>
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- XML 基础教程 课后 习题 解答