汽车零配件生产企业ERP—订单管理子系统外文翻译资料

 2022-10-31 14:41:55

Core Javareg; Volume II—Advanced Features

Cay S. Horstmann

Gary Cornell

The preface of the book Essential XML by Don Box et al. (Addison-Wesley, 2000) states only half-jokingly: “The Extensible.Markup Language (XML) has replaced Java, Design Patterns, and Object Technology as the software industryrsquo;s solution to world hunger.” Indeed, as you will see in this chapter, XML is a very useful technology for describing structured information. XML tools make it easy to process and transform that information. However, XML is not a silver bullet. You need domain-specific standards and code libraries to use it effectively. Moreover, far from making Java technology obsolete, XML works very well with Java. Since the late 1990s, IBM, Apache, and others have been instrumental in producing high-quality Java libraries for XML processing. Many of these libraries have now been integrated into the Java platform.

This chapter introduces XML and covers the XML features of the Java library. As always, wersquo;ll point out along the way when the hype surrounding XML is justified—and when you have to take it with a grain of salt and try solving your problems the old-fashioned way, through good design and code.

2.1. Introducing XML

In Chapter 10 of Volume I, you have seen the use of property files to describe the configuration of a program. A property

file contains a set of name/value pairs, such as

fontname=Times Roman

fontsize=12

windowsize=400 200

color=0 50 100

You can use the Properties class to read in such a file with a single method call. Thatrsquo;s a nice feature, but it doesnrsquo;t

really go far enough. In many cases, the information you want to describe has more structure than the property file format can comfortably handle. Consider the fontname/fontsize entries in the example. It would be more object-oriented to have a single entry:

font=Times Roman 12

But then, parsing the font description gets ugly as you have to figure out when the font name ends and the font size starts.

Property files have a single flat hierarchy. You can often see programmers work around that limitation with key names, such as

title.fontname=Helvetica

title.fontsize=36

body.fontname=Times Roman

body.fontsize=12

Another shortcoming of the property file format is the requirement that keys must be unique. To store a sequence of values,you need another workaround, such as

menu.item.1=Times Roman

menu.item.2=Helvetica

menu.item.3=Goudy Old Style

The XML format solves these problems. It can express hierarchical structures and is thus more flexible than the flat table structure of a property file.

An XML file for describing a program configuration might look like this:

lt;configurationgt;

lt;titlegt;

lt;fontgt;

lt;namegt;Helveticalt;/namegt;

lt;sizegt;36lt;/sizegt;

lt;/fontgt;

lt;/titlegt;

lt;bodygt;

lt;fontgt;

lt;namegt;Times Romanlt;/namegt;

lt;sizegt;12lt;/sizegt;

lt;/fontgt;

lt;/bodygt;

lt;windowgt;

lt;widthgt;400lt;/widthgt;

lt;heightgt;200lt;/heightgt;

lt;/windowgt;

lt;colorgt;

lt;redgt;0lt;/redgt;

lt;greengt;50lt;/greengt;

lt;bluegt;100lt;/bluegt;

lt;/colorgt;

lt;menugt;

lt;itemgt;Times Romanlt;/itemgt;

lt;itemgt;Helveticalt;/itemgt;

lt;itemgt;Goudy Old Stylelt;/itemgt;

lt;/menugt;

lt;/configurationgt;

The XML format allows you to express the hierarchy and record repeated elements without contortions.

The format of an XML file is straightforward. It looks similar to an HTML file. There is a good reason for that—both the XML and HTML formats are descendants of the venerable Standard Generalized Markup Language (SGML).

SGML has been around since the 1970s for describing the structure of complex documents. It has been used with success in some industries that require ongoing maintenance of massive documentation—in particular, the aircraft industry. However, SGML is quite complex, so it has never caught on in a big way. Much of that complexity arises because SGML has two conflicting goals.

SGML wants to make sure that documents are formed according to the rules for their document type, but it also wants to make data entry easy by allowing shortcuts that reduce typing. XML was designed as a simplified version of SGML for use on the Internet. As is often true, simpler is better, and XML has enjoyed the immediate and enthusiastic reception that has eluded SGML for so long.

Note

You can find a very nice version of the XML standard, with annotations by Tim Bray, at www.xml.com/axml/axml.html.

Even though XML and HTML have common roots, there are important differences between the two.

bull; Unlike HTML, XML is case-sensitive. For example, lt;H1gt; and lt;h1gt; are different XML tags.

bull; In HTML, you can omit end tags, such as lt;/pgt; or lt;/ligt;, if it is clear from the context where a paragraph or list

item ends. In XML, you can never omit an end tag.

bull; In XML, elements that have a single tag without a matching end tag must end in a /, as in lt;img src='coffeecup.png'/gt;. That way, the parser knows not to look for a lt;/imggt; tag.

bull; In XML, attribute values must be enclosed in quotation marks. In HTML, quotation marks are optional. For example,

lt;applet code='MyApplet.class' width=300 height=300gt; is legal HTML but not legal XML. In XML, you have to use quotation marks: width='300'.

bull; In HTML, you can have attribute names without values, such as lt;input type='radio' name='language' value='Java' checkedgt;. In XML, all attributes must have values, such as checked='true' or (ugh) checked='checked'.

剩余内容已隐藏,支付完成后下载完整资料


Java核心技术卷Ⅱ—高级特性

Cay S. Horstmann

Gary Cornell

Don Box 等人在其合著的《Essential XML》(Addison-Wesley 出版社 2000 年出版) 的前言中半开玩笑地说道:“可扩展标记语言(Extensible Markup Language, XML) 已经取代了Java、 设计模式、 对象技术, 成为软件行业解决世界饥荒的方案。” 确实, 正如你将在本章看到的, XML 是一个非常有用的描述结构化信息的技术。 XML 工具使处理和转化信息十分容易。 但是, XML 并不是银弹。 需要领域相关的标准和代码库才能有效地使用 XML。 此外,XML 非但没有使 Java 技术过时, 还与 Java 配合得很好。 从 20 世纪 90 年代末以来, IBM、Apache 和其他许多公司一直在帮助开发用于 XML 处理的高质量 Java 库, 其中大部分重要的代码库都整合到了 Java 平台中。

本章将介绍 XML, 并涵盖了 Java 库的 XML 特性。 一如既往, 我们将指出何时大量地使用 XML 是正确的; 而何时必须有保留地使用 XML, 通过利用良好的设计和代码, 来采用老办法解决问题。

2.1 XML 概述

在第Ⅰ卷第 10 章中, 你已经看见过用属性文件(property file) 来描述程序配置。 属性文件包含了一组名 / 值对, 例如:

fontname=Times Roman

fontsize=12

windowsize=400 200

color=0 50 100

你可以在 Properties 类中仅使用一个方法调用即可读入这样的属性文件。 这是一个很好的特性, 但这还不够。 在许多情况下, 想要描述的信息的结构比较复杂, 属性文件不能很方便地处理它。 例如, 对于下面例子中的 fontname/fontsize 项, 使用以下的单一项将更符合面向对象的要求:

font=Times Roman 12

但是, 这时对字体描述的分析就变得很讨厌了, 必须确定字体名在何处结束, 字体大小在何处开始。属性文件采用的是一种单一的平面层次结构。 你常常会看到程序员用如下的键名来努力解决这种局限性:

title.fontname=Helvetica

title.fontsize=36

body.fontname=Times Roman

body.fontsize=12

属性文件格式的另一个缺点是要求键是惟一的。 如果要存放一个值序列, 则需要另一个变通方法, 例如:

menu.item.1=Times Roman

menu.item.2=Helvetica

menu.item.3=Goudy Old Style

XML 格式解决了这些问题, 因为它能够表示层次结构, 这比属性文件的平面表结构更灵活。描述程序配置的 XML 文件可能会像这样:

lt;configurationgt;

lt;titlegt;

lt;fontgt;

lt;namegt;Helveticalt;/namegt;

lt;sizegt;36lt;/sizegt;

lt;/fontgt;

lt;/titlegt;

lt;bodygt;

lt;fontgt;

lt;namegt;Times Romanlt;/namegt;

lt;sizegt;12lt;/sizegt;

lt;/fontgt;

lt;/bodygt;

lt;windowgt;

lt;widthgt;400lt;/widthgt;

lt;heightgt;200lt;/heightgt;

lt;/windowgt;

lt;colorgt;

lt;redgt;0lt;/redgt;

lt;greengt;50lt;/greengt;

lt;bluegt;100lt;/bluegt;

lt;/colorgt;

lt;menugt;

lt;itemgt;Times Romanlt;/itemgt;

lt;itemgt;Helveticalt;/itemgt;

lt;itemgt;Goudy Old Stylelt;/itemgt;

lt;/menugt;

lt;/configurationgt;

XML 格式能够表达层次结构, 并且重复的元素不会被曲解。

正如上面看到的, XML 文件的格式非常直观, 它与 HTML 文件非常相似。 这完全是有理由的, 因为 XML 和 HTML 格式是古老的标准通用标记语言(Standard Generalized Markup Language, SGML) 的衍生语言。

SGML 从 20 世纪 70 年代开始就用于描述复杂文件的结构。 它的使用在一些要求对海量文献进行持续维护的产业中取得了成功, 特别是在飞机制造业中。 但是, SGML 相当复杂,所以它从未风行。 造成 SGML 如此复杂的主要原因是 SGML 有两个相互矛盾的目标。 它既想要确保文档能够根据其文档类型的规则来形成, 又想要通过减少数据键入的快捷方式使数据项变得容易表示。 XML 设计成了一个用于因特网的 SGML 的简化版本。 和通常情况一样,越简单的东西越好, XML 立即得到了长期以来一直在躲避 SGML 的用户的热情追捧。

注意

在 http://www.xml.com/axml/axml.html 可以找到一个由 Tim Bray 注释的 XML 标准的极佳版本。

尽管 HTML 和 XML 同宗同源, 但是两者之间存在着重要的区别:

● 与 HTML 不同, XML 是大小写敏感的。 例如, lt;H1gt; 和 lt;h1gt; 是不同的 XML 标签。

● 在 HTML 中, 如果从上下文可以分清哪里是段落或列表项的结尾, 那么结束标签(如lt;/pgt; 或 lt;/ligt;) 就可以省略, 而在 XML 中结束标签绝对不能省略。

● 在 XML 中, 只有单个标签而没有相对应的结束标签的元素必须以 / 结尾, 比如 lt;img src='coffeecup.png'/gt;。 这样, 解析器就知道不需要查找 lt;/imggt; 标签了。

● 在 XML 中, 属性值必须用引号括起来。 在 HTML 中, 引号是可有可无的。 例如,lt;applet code='MyApplet.class' width=300 height=300gt; 对 HTML 来说是合法的,但是对 XML 来说则是不合法的。 在 XML 中, 必须使用引号, 比如, width='300'。

● 在 HTML 中, 属 性 名 可 以 没 有 值。 例 如, lt;input type='radio' name='language' value='Java' checkedgt;。 在 XML 中, 所 有 属 性 必 须 都 有 属 性 值。 比 如,checked='true' 或 checked='checked'。

XML 文档的结构

XML 文档应当以一个文档头开始, 例如:

lt;?xml version='1.0'?gt;

或者

lt;?xml version='1.0' encoding='UTF-8'?gt;

严格来说, 文档头是可选的, 但是强烈推荐你使用文档头。

注意:

因为建立 SGML 是为了处理真正的文档, 因此 XML 文件叫做文档, 尽管许多XML 文件是用来描述通常不称作文档的数据集的。

文档头之后通常是文档类型定义(Document Type Definition, DTD), 例如:

lt;!DOCTYPE web-app PUBLIC

'-//Sun Microsystems, Inc.//DTD Web Application 2.2/

'http://java.sun.com/j2ee/dtds/web-app_2_2.dtd'gt;

文档类型定义的是确保文档正确的一个重要机制, 但是它不是必需的。 我们将在本章的后面讨论这个问题。

最后, XML 文档的正文包含根元素, 根元素包含其他元素。 例如:

lt;?xml version='1.0'?gt;

lt;!DOCTYPE configuration . . .gt;

lt;configurationgt;

lt;titlegt;

lt;fontgt;

lt;namegt;Helveticalt;/namegt;

lt;sizegt;36lt;/sizegt;

lt;/fontgt;

lt;/titlegt;

. . .

lt;/configurationgt;

元素可以有子元素(child element)、 文本或两者皆有。 在上述例子中, font 元素有两个子元素, 它们是 name 和 size。 name 元素包含文本“Helvetica”。

提示: 在设计 XML 文档结构时, 最好使元素要么包含子元素, 要么包含文本。 换句话说, 你应该避免以下情况:

lt;fontgt;

Helvetica

lt;sizegt;36lt;/sizegt;

lt;/fontgt;

在 XML 规范中, 这叫做混合式内容(mixed content)。 本章中, 稍后你将会看到, 如果避免了混合式内容, 就可以简化解析过程。

XML 元素可以包含属性, 例如:

lt;size unit='pt'gt;36lt;/sizegt;

何时用元素, 何时用属性, 在 XML 设计人员中存在一些分歧。 例如, 将 font 做如下描述:

lt;font name='Helvetica' size='36'/gt;

似乎比下面更简单一些:

lt;fontgt;

lt;namegt;Helveticalt;/namegt;

lt;sizegt;36lt;/sizegt;

lt;/fontgt;

但是, 属性的灵活性要差很多。 假设你想把单位添加到 size 的值中去, 如果使用属性, 那么必须把单位添加到属性值中去:

lt;font name='Helvetica' size='36 pt'/gt;

嗨! 现在必须对字符串“36 pt” 进行解析, 而这正是 XML 设计用来避免的那种麻烦。把属性加到 size 元素中则简单多了:

lt;fontgt;

lt;namegt;Helveticalt;/namegt;

lt;size unit='pt'gt;36lt;/sizegt;

lt;/fontgt;

一个常用的经验法则是, 属性只应该用来修改值的解释, 而不是用来指定值。 如果你发现自己陷入了争论, 在纠结于某个设置是否是对某个值的解释所作的修改, 那么你就应该对属性说“不”, 转而使用元素, 许多有用的文档根本就不使用属性。

注意: 在 HTML 中属性的使用规则很简单: 凡是不显示在网页上的都是属性。 例如以下的超链接:

lt;a href='http://java.sun.com'gt;Java Technologylt;/agt;

字符串 Java Technology 要在网页上显示, 但是这个链接的 URL 并不是显示页面

的一部分。 然而, 这个规则对于大多数 XML 并不那么管用, 因为 XML 文件中的数据并非像通常意义那样是让人浏览的。

元素和文本是 XML 文档“主要的支撑要素”, 你可能还会遇到的其他一些标记, 说明如下:

● 字符引用(character reference) 的形式是 amp;# 十进制值 ; 或 amp;#x 十六进制值 ;。 例如, 字符 eacute; 可以用下面两种形式表示:

amp;#233;amp;#xE9

● 实体引用(entity reference) 的形式是 amp;name;。 下面这些实体引用:

amp;lt;amp;gt;amp;amp;amp;quot;amp;apos;

都有预定义的含义: 小于、 大于、 amp;、 引号、 省略号等字符

剩余内容已隐藏,支付完成后下载完整资料


资料编号:[141726],资料为PDF文档或Word文档,PDF文档可免费转换为Word

原文和译文剩余内容已隐藏,您需要先支付 30元 才能查看原文和译文全部内容!立即支付

以上是毕业论文外文翻译,课题毕业论文、任务书、文献综述、开题报告、程序设计、图纸设计等资料可联系客服协助查找。