当前位置:网站首页>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 %StatusEnd 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 .
边栏推荐
- The difference and usage between substr (), slice (), and substring () in the string interception methods of "understand series after reading"
- 1672. Total assets of the richest customers
- [uniapp] uniapp development app online Preview PDF file
- 奥迪AUDI EDI INVOIC发票报文详解
- MXNet对GoogLeNet的实现(并行连结网络)
- 测试工程师如何“攻城”(下)
- Scala basic tutorial -- 13 -- advanced function
- 整理混乱的头文件,我用include what you use
- The CDC of sqlserver can read the data for the first time, but it can't read the data after adding, deleting and modifying. What's the reason
- 资料下载 丨首届腾讯技术开放日课程精华!
猜你喜欢

Don't just learn Oracle and MySQL!

Lex and yacc based lexical analyzer + parser

Scala basic tutorial -- 20 -- akka

Use canal and rocketmq to listen to MySQL binlog logs
redis分布式锁的8大坑总结梳理

Nature microbiology | viral genomes in six deep-sea sediments that can infect Archaea asgardii

Scala基础教程--19--Actor

Scala基础教程--16--泛型

2022年字节跳动日常实习面经(抖音)

基于lex和yacc的词法分析器+语法分析器
随机推荐
Scala basic tutorial -- 12 -- Reading and writing data
The CDC of sqlserver can read the data for the first time, but it can't read the data after adding, deleting and modifying. What's the reason
完善的js事件委托
PB的扩展DLL开发(超级篇)(七)
大div中有多个div,这些div在同一行显示,溢出后产生滚动条而不换行
请教一下 flinksql中 除了数据统计结果是状态被保存 数据本身也是状态吗
2014 Hefei 31st youth informatics Olympic Games (primary school group) test questions
DeFi生态NFT流动性挖矿系统开发搭建
Leetcode fizzbuzz C # answer
2021 Hefei informatics competition primary school group
Oracle with as ORA-00903: invalid table name 多表报错
2014合肥市第三十一届青少年信息学奥林匹克竞赛(小学组)试题
与二值化阈值处理相关的OpenCV函数、方法汇总,便于对比和拿来使用
测试工程师如何“攻城”(下)
SSL证书续费相关问题详解
Download the first Tencent technology open day course essence!
2022CoCa: Contrastive Captioners are Image-Text Fountion Models
信息学奥赛一本通 1336:【例3-1】找树根和孩子
基于C语言的菜鸟驿站管理系统
Unity adds a function case similar to editor extension to its script, the use of ContextMenu