第四章 将对象映射到 XML - 例外
例外
如果给定属性未映射为 XML
,则无需为其引用的类启用 XML
。以下属性不会映射到 XML
:
- 私有属性
- 多维属性
- 将
XMLPROJECTION
参数指定为NONE
的属性
如果将 XMLPROJECTION
属性参数设置为“ELEMENT
”或其他适当的值,则可以映射私有属性和多维属性。请注意,对于多维属性,只有顶部节点可以包含在映射中。
默认映射总结
默认的 XML
映射如下:
-
一个对象实例对应于一个顶级
XML
元素。 -
仅映射属性。没有其他类成员被映射。
此外,私有属性和多维属性也会被忽略。
-
属性按它们在
Studio
中出现的相同顺序投影到XML
。 -
任何没有指定类型的属性都被假定为字符串。
-
每个对象值属性都对应于封闭的顶级
XML
元素中的一个XML
元素。它的属性嵌套在该元素内。
-
关系的处理方式与列表属性相同。
XML
映射仅包含关系的一侧;如果尝试映射两侧,则会发生错误。 -
字符流被映射为字符串。
-
二进制流使用采用
Base-64
编码的字符串进行映射。
XML
映射示例
本节显示支持 XML
的类及其 XML
映射。
支持 XML
的类示例
下面显示了一个支持 XML
的类,其中包括主要的结构属性变体:
Class Basics.BasicDemo Extends (%RegisteredObject, %XML.Adaptor)
{
Parameter XMLTYPENAMESPACE = "mytypes";
Property SimpleProp As %String;
Property ObjProp As SimpleObject;
Property Collection1 As list Of %String;
Property Collection2 As list Of SimpleObject;
Property MultiDimProp As %String [ MultiDimensional ];
Property PrivateProp As %String [ Private ];
}
XMLTYPENAMESPACE
参数指定此类中定义的类型的目标命名空间。
SimpleObject
类也支持 XML
:
Class Basics.SimpleObject Extends (%RegisteredObject, %XML.Adaptor)
{
Parameter XMLTYPENAMESPACE = "mytypes";
Property MyProp As %String;
Property AnotherProp As %String;
}
XML
文档示例
下面显示了从 BasicDemo
类的实例生成的 XML
文档:
<?xml version="1.0" encoding="UTF-8"?>
<BasicDemo>
<SimpleProp>abc</SimpleProp>
<ObjProp>
<MyProp>12345</MyProp>
<AnotherProp>67890</AnotherProp>
</ObjProp>
<Collection1>
<Collection1Item>list item 1</Collection1Item>
<Collection1Item>list item 2</Collection1Item>
</Collection1>
<Collection2>
<SimpleObject>
<MyProp>12345</MyProp>
<AnotherProp>67890</AnotherProp>
</SimpleObject>
<SimpleObject>
<MyProp>12345</MyProp>
<AnotherProp>67890</AnotherProp>
</SimpleObject>
</Collection2>
</BasicDemo>
示例架构
下面显示了两个示例类中使用的 XML
类型命名空间的架构:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:s="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" targetNamespace="mytypes">
<complexType name="BasicDemo">
<sequence>
<element minOccurs="0" name="SimpleProp" type="s:string"/>
<element minOccurs="0" name="ObjProp" type="s01:SimpleObject" xmlns:s01="mytypes"/>
<element minOccurs="0" name="Collection1" type="s02:ArrayOfCollection1ItemString" xmlns:s02="mytypes"/>
<element minOccurs="0" name="Collection2" type="s03:ArrayOfSimpleObjectSimpleObject" xmlns:s03="mytypes"/>
</sequence>
</complexType>
<complexType name="SimpleObject">
<sequence>
<element minOccurs="0" name="MyProp" type="s:string"/>
<element minOccurs="0" name="AnotherProp" type="s:string"/>
</sequence>
</complexType>
<complexType name="ArrayOfCollection1ItemString">
<sequence>
<element maxOccurs="unbounded" minOccurs="0" name="Collection1Item" nillable="true" type="s:string"/>
</sequence>
</complexType>
<complexType name="ArrayOfSimpleObjectSimpleObject">
<sequence>
<element maxOccurs="unbounded" minOccurs="0" name="SimpleObject"
nillable="true" type="s04:SimpleObject" xmlns:s04="mytypes"/>
</sequence>
</complexType>
</schema>
网友评论