以下文為基礎,進行分析
Long Term Persistence of JavaBeans Components: XML Schema
http://java.sun.com/products/jfc/tsc/articles/persistence3/
1Basic Elements
每個xml以一個可選的
<?xml version="1.0" encoding="UTF-8" ?>
開頭,接著是
<java version="1.4.0" class="java.beans.XMLDecoder">
...objects go here...
</java>
其中,version,class這個兩個屬性似乎已經不使用了。
<java>..</java>中間填充的是組成xml文檔的objects,以被解碼器readObject的順序出現。
2Objects
每個object由創建它的一系列方法,按序排列來代表。
每個xml元素都代表一個方法調用。這些方法:一,創建了object(對應一個表達式expression);二,影響了object(對應一個語句statement)。
strings被看作特殊的expressions,標識符(identifiers)用來在object創建之后再引用object時使用的。
3Strings
xml中的原子表達式,
<string>Hello, World</string>
The '<' and '&' characters are represented by the < and & escape sequences.
4Expressions and Statements
expression是有返回值的方法調用,表示為:<object>
statement是沒有返回值的方法調用。表示為:<void>
兩個元素都有一個method屬性,表示調用的方法。
<object>的class 屬性指定作為一個靜態方法的目標的class,構造函數表示為:一個名為new的靜態方法。
當expression,statement進一步還包含expressions,則這些expressions作為前兩者所代表的方法的參數。
所以,
<object class="javax.swing.JButton" method="new">
<string>Press me</string>
</object>
代表的java 語句是
new JButton("Press me");
還可以表示為:
<object class="javax.swing.JButton">
<string>Press me</string>
</object>
因為,new是默認的方法。
將影響一個object(由expression產生)的方法(一個statement)放到這個object里去,如下:
<object class="javax.swing.JButton">
<void method="setText">
<string>Hello, world</string>
</void>
</object>
即為:
JButton b = new JButton();
b.setText("Hello, world");
If an expression should not be used as an argument to the enclosing method, it should be represented with the <void> tag. The result of an expression in a <void> tag is still evaluated and used by any objects it encloses.(待考)
嵌套expression,statement的能力減少了描述圖形時使用的identifier的數目。
5標識符
有時,用<object>聲明了一個對象之后,可能會在其他地方再次用到該對象,所以要定義identifier:
<object id="button1" class="javax.swing.JButton"/>
id是全局的,從聲明起。
例子:
<object class="javax.swing.JPanel">
<void method="add">
<object id="button1" class="javax.swing.JButton"/>
</void>
<void method="add">
<object class="javax.swing.JLabel">
<void method="setLabelFor">
<object idref="button1"/>
</void>
</object>
</void>
</object>
即為:
JPanel panel1 = new JPanel();
JButton button1 = new JButton();
JLabel label1 = new JLabel();
panel1.add(button1);
panel1.add(label1);
label1.setLabelFor(button1);
id還可以這樣用:
<object class="java.util.Date">
<void id="now" method="getTime"/>
</object>
It allows an expression to be evaluated in the context of the enclosing instance, in this case defining the variable now as the value of the expression. It corresponds to the following Java code: (待考)
long now = new Date().getTime();
6縮寫詞Abbreviation
上述知識,足夠使用XMLEncoder,下面是高級知識,可以讓xml更簡單:
primitives
The following tags represent both the primitive types and their corresponding wrapper classes:
<boolean>
<byte>
<char>
<short>
<int>
<long>
<float>
<double>
例如:
<object class="java.lang.Integer">
<string>123</string>
</object>
可簡寫為:
<int>123</int>
null
<null/>
class
The <class> tag can be used to represent an instance of Class. For example,
<object class="java.lang.Class method="forName">
<string>java.awt.event.ActionListener</string>
</object>
is shortened to
<class>java.awt.event.ActionListener</class>
which is equivalent to ActionListener.class.
Static Constants
(only in releases after 1.4.0 beta) (待考)
As of the release following 1.4.0 beta, the values of static constants may be written using the class and field attributes to specify the declaring class and field name of the constant, respectively. Thus
<void class="javax.swing.JTable" method="getField">
<string>AUTO_RESIZE_OFF</string>
<void id="Integer0" method="get">
<null/>
</void>
</void>
<object idref="Integer0"/>
is shortened to
<object class="javax.swing.JTable" field="AUTO_RESIZE_OFF"/>
property
以get,set開頭的方法可以簡寫:
如,<void method="getText"/>
is shortened to:
<void property="text"/>
如,
<void method="setText">
<string>Hello, world</string>
</void>
is shortened to:
<void property="text">
<string>Hello, world</string>
</void>
Index
繼承于java.util,list接口的get,set方法可以簡寫為:
<void method="get">
<int>3</int>
<void>
is shortened to
<void index="3"/>
which corresponds to the following Java code:
Object o = aList.get(3);
又,
<void index="3">
<string>Hello, world</string>
</void>
is equivalent to
<void method="set">
<int>3</int>
<string>Hello, world</string>
</void>
which corresponds to the following Java code:
aList.set(3, "Hello, world")
array
用<array>來表示數組,
<array class="java.awt.Component" length="3"/>
It corresponds to the following Java code:
Component[] a = new Component[3];
version1。4之后,還可以:
<array class="int">
<int>123</int>
<int>456</int>
</array>
represents the following Java code fragment:
int[] intArray = {123, 456};
which represents JTable.AUTO_RESIZE_OFF.
The Top Level environment
定義在<java></java>這個層次的一些高級屬性,涉及到XMLDecoder(待考)
7DTD
http://java.sun.com/products/jfc/tsc/articles/persistence3/javabeans.dtd
一個例子:
http://java.sun.com/products/jfc/tsc/articles/persistence3/Browse.xml
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.0" class="java.beans.XMLDecoder">
<object class="javax.swing.JFrame">
<void id="JPanel0" property="contentPane">
<void method="add">
<object id="JLabel0" class="javax.swing.JLabel">
<void property="text">
<string>URL:</string>
</void>
<void property="location">
<object class="java.awt.Point">
<int>10</int>
<int>10</int>
</object>
</void>
</object>
</void>
<void method="add">
<object id="JScrollPane0" class="javax.swing.JScrollPane">
<void property="preferredSize">
<object class="java.awt.Dimension">
<int>500</int>
<int>300</int>
</object>
</void>
<void property="viewport">
<void method="add">
<object id="JEditorPane0" class="javax.swing.JEditorPane"/>
</void>
</void>
<void property="location">
<object class="java.awt.Point">
<int>10</int>
<int>40</int>
</object>
</void>
</object>
</void>
<void method="add">
<object id="JTextField0" class="javax.swing.JTextField">
<void property="text">
<string>file:///C:/</string>
</void>
<void method="addActionListener">
<object class="java.beans.EventHandler" method="create">
<class>java.awt.event.ActionListener</class>
<object idref="JEditorPane0"/>
<string>page</string>
<string>source.text</string>
</object>
</void>
<void property="location">
<object class="java.awt.Point">
<int>62</int>
<int>10</int>
</object>
</void>
</object>
</void>
<void property="layout">
<object class="javax.swing.SpringLayout">
<void method="putConstraint">
<string>East</string>
<object idref="JTextField0"/>
<int>0</int>
<string>East</string>
<object idref="JScrollPane0"/>
</void>
<void method="putConstraint">
<string>West</string>
<object idref="JTextField0"/>
<int>10</int>
<string>East</string>
<object idref="JLabel0"/>
</void>
<void method="putConstraint">
<string>South</string>
<object idref="JPanel0"/>
<int>10</int>
<string>South</string>
<object idref="JScrollPane0"/>
</void>
<void method="putConstraint">
<string>East</string>
<object idref="JPanel0"/>
<int>10</int>
<string>East</string>
<object idref="JScrollPane0"/>
</void>
<void method="putConstraint">
<string>North</string>
<object idref="JScrollPane0"/>
<int>40</int>
<string>North</string>
<object idref="JPanel0"/>
</void>
</object>
</void>
</void>
<void method="pack"/>
<void property="visible">
<boolean>true</boolean>
</void>
</object>
</java>
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
