当前位置:网站首页>Generate XML elements
Generate XML elements
2022-07-04 19:24:00 【User 7741497】
Generate XML Elements
If you use RootElement()
Start the root element of the document , Is responsible for generating each element in the root element . There are three options :
Generate objects as elements
It can be downloaded from InterSystems IRIS Object generates output as an element . In this case , Use object()
Method , This method supports writing XML The object of . The output includes all object references contained in the object . You can specify the name of this element , You can also use the default values defined in the object .
Only in RootElement()
and EndRootElement()
Use between methods object()
Method .
This example is enabled for a given XML All saved instances of the class of generate output :
/// desc: Output the data in the table to the local file
/// w ##class(PHA.TEST.Xml).WriteAll("Sample.Person")
ClassMethod WriteTableAllToXml(cls As %String = "", directory As %String = "E:\temp\")
{
if '##class(%Dictionary.CompiledClass).%ExistsId(cls) {
Write !, " Class does not exist or is not compiled "
Quit
}
s check=$classmethod(cls, "%Extends", "%XML.Adaptor")
If 'check {
Write !, " Class does not extend %XML.Adaptor"
Quit
}
s filename = directory_"Person"_".xml"
s writer = ##class(%XML.Writer).%New()
s writer.Indent=1
s status = writer.OutputToFile(filename)
if $$$ISERR(status) { do $System.Status.DisplayError(status) quit }
s status=writer.RootElement("SampleOutput")
if $$$ISERR(status) { do $System.Status.DisplayError(status) quit }
// Get the ID
s stmt = ##class(%SQL.Statement).%New()
s status = stmt.%PrepareClassQuery(cls,"Extent")
if $$$ISERR(status) { do $System.Status.DisplayError(status) quit }
s rset = stmt.%Execute()
while (rset.%Next()) {
// For each ID, Write this object
set objid = rset.%Get("ID")
set obj = $CLASSMETHOD(cls,"%OpenId",objid)
set status = writer.Object(obj)
if $$$ISERR(status) {Do $System.Status.DisplayError(status) Quit}}
d writer.EndRootElement()
d writer.EndDocument()
q ""
}
The output of this method contains all the saved objects of the given class , These objects are nested in the root element . about Sample.Person
, Output is as follows :
<?xml version="1.0" encoding="UTF-8"?>
<SampleOutput>
<Person>
<Name>Tillem,Robert Y.</Name>
<SSN>967-54-9687</SSN>
<DOB>1961-11-27</DOB>
<Home>
<Street>3355 First Court</Street>
<City>Reston</City>
<State>WY</State>
<Zip>11090</Zip>
</Home>
<Office>
<Street>4922 Main Drive</Street>
<City>Newton</City>
<State>NM</State>
<Zip>98073</Zip>
</Office>
<FavoriteColors>
<FavoriteColorsItem>Red</FavoriteColorsItem>
</FavoriteColors>
<Age>47</Age>
</Person>
<Person>
<Name>Waters,Ed X.</Name>
<SSN>361-66-2801</SSN>
<DOB>1957-05-29</DOB>
<Home>
<Street>5947 Madison Drive</Street>
...
</SampleOutput>
Build elements manually
Constructed manually XML Elements . In this case , Use element()
Method , This method writes the start tag of the element with the supplied name . then , You can write content 、 Attributes and child elements . Use EndElement()
Method indicates the end of the element .
The relevant methods are as follows :
Element()
method Element(tag, namespace As %String) as %Status
Write start tag . You can provide namespaces for elements , Only when XML There is no class for Namespace
The namespace is applied only when the value of the parameter .
WriteAttribute()
method WriteAttribute(name As %String,
value As %String = "",
namespace As %String,
valueNamespace As %String = "",
global As %Boolean = 0) as %Status
Write properties . Attribute name and value must be specified . The parameter namespace is the namespace of the attribute name . Parameters valueNamespace
Is the namespace of the attribute value ; On duty XML When defined in the schema namespace .
about GLOBAL, If the attribute is in the associated XML Architecture is global , So there should be a prefix , Please specify TRUE.
If you use this method , Must be in Element()
( or RootElement()
) Then use it directly .
WriteChars()
method WriteChars(text) as %Status
Write string , Perform any necessary escape required to make the string fit as element content . Parameter must %String
Type or %CharacterStream
type .
WriteCData()
method WriteCData(text) as %Status
Parameter must %String
Type or %CharacterStream
type .
WriteBase64()
method WriteBase64(binary) as %Status
Encode the specified binary bytes as base-64
, And write the result text into the content of the element . The parameter must be of type %Binary
or %BinaryStream
.
WriteBinHex()
method WriteBinHex(binary) as %Status
Encode the specified binary bytes into binary , And write the result text into the content of the element . The parameter must be of type %Binary
or %BinaryStream
.
EndElement()
method EndElement() as %Status
End the element that can match it .
Only in RootElement()
and EndRootElement()
Use these methods between methods .
Be careful : The method described here is intended to enable to XML Document specific logical fragments , But in some cases , More control may be required .%XML.Writer
Class provides an additional method write()
, You can use this method to write any string . Responsible for ensuring that the results are well formed XML file ; No verification is provided .
Example
Here is an example routine :
/// w ##class(Demo.XmlDemo).WriteObjXml()
ClassMethod WriteObjXml()
{
set writer=##class(%XML.Writer).%New()
set writer.Indent=1
set status=writer.OutputToDevice()
if $$$ISERR(status) {do $System.Status.DisplayError(status) quit}
set status=writer.StartDocument()
if $$$ISERR(status) {do $System.Status.DisplayError(status) quit}
set status=writer.RootElement("root")
if $$$ISERR(status) {do $System.Status.DisplayError(status) quit}
set status=writer.Element("SampleElement")
if $$$ISERR(status) {do $System.Status.DisplayError(status) quit}
set status=writer.WriteAttribute("Attribute","12345")
if $$$ISERR(status) {do $System.Status.DisplayError(status) quit}
set status=writer.Element("subelement")
if $$$ISERR(status) {do $System.Status.DisplayError(status) quit}
set status=writer.WriteChars("yao")
if $$$ISERR(status) {do $System.Status.DisplayError(status) quit}
set status=writer.EndElement()
if $$$ISERR(status) {do $System.Status.DisplayError(status) quit}
set status=writer.Element("subelement")
if $$$ISERR(status) {do $System.Status.DisplayError(status) quit}
set status=writer.WriteChars("xin")
if $$$ISERR(status) {do $System.Status.DisplayError(status) quit}
set status=writer.EndElement()
if $$$ISERR(status) {do $System.Status.DisplayError(status) quit}
set status=writer.EndElement()
if $$$ISERR(status) {do $System.Status.DisplayError(status) quit}
set status=writer.EndRootElement()
if $$$ISERR(status) {do $System.Status.DisplayError(status) quit}
set status=writer.EndDocument()
if $$$ISERR(status) {do $System.Status.DisplayError(status) quit}
q ""
}
DHC-APP>w ##class(Demo.XmlDemo).WriteObjXml()
<?xml version="1.0" encoding="UTF-8"?>
<root>
<SampleElement Attribute="12345">
<subelement>yao</subelement>
<subelement>xin</subelement>
</SampleElement>
</root>
Use %XMLL.Element
In the previous section , We used Element() And specify the elements to be generated ; We can also specify namespaces . In some cases , Class %XML.Element Example , Instead of using element names . This class has the following properties :
- Local Property specifies whether this element is a local element of its parent element , This will affect the control of namespaces .
- Namespace Attribute specifies the namespace of this element .
- Tagname Attribute specifies the name of this element .
You can also use the above described WriteAttribute() Method .
边栏推荐
- Scala基础教程--17--集合
- Technology sharing | interface testing value and system
- Wireshark网络抓包
- 2022-07-04:以下go语言代码输出什么?A:true;B:false;C:编译错误。 package main import 'fmt' func
- OpenCV的二值化处理函数threshold()详解
- LM10丨余弦波动顺势网格策略
- 物联网应用技术的就业前景和现状
- Build your own website (15)
- 2022健康展,北京健博会,中国健康展,大健康展11月13日
- 更安全、更智能、更精致,长安Lumin完虐宏光MINI EV?
猜你喜欢
Lex and yacc based lexical analyzer + parser
整理混乱的头文件,我用include what you use
读写关闭的channel是啥后果?
Scala basic tutorial -- 18 -- set (2)
神经网络物联网应用技术学什么
Summary and sorting of 8 pits of redis distributed lock
One question per day (2022-07-02) - Minimum refueling times
Nature microbiology | viral genomes in six deep-sea sediments that can infect Archaea asgardii
My colleagues quietly told me that flying Book notification can still play like this
更安全、更智能、更精致,长安Lumin完虐宏光MINI EV?
随机推荐
2014合肥市第三十一届青少年信息学奥林匹克竞赛(小学组)试题
Scala基础教程--17--集合
Scala基础教程--16--泛型
Scala基础教程--19--Actor
26. 删除有序数组中的重复项 C#解答
Scala基础教程--14--隐式转换
Wireshark网络抓包
2022健康展,北京健博会,中国健康展,大健康展11月13日
C#实现定义一套中间SQL可以跨库执行的SQL语句(案例详解)
英特尔集成光电研究最新进展推动共封装光学和光互连技术进步
Nebula Importer 数据导入实践
基于lex和yacc的词法分析器+语法分析器
LeetCode FizzBuzz C#解答
Lex and yacc based lexical analyzer + parser
ESP32-C3入门教程 问题篇⑫——undefined reference to rom_temp_to_power, in function phy_get_romfunc_addr
C language printing exercise
Is Guoyuan futures a regular platform? Is it safe to open an account in Guoyuan futures?
[release] a tool for testing WebService and database connection - dbtest v1.0
Scala basic tutorial -- 13 -- advanced function
Scala basic tutorial -- 17 -- Collection