当前位置:网站首页>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 .
边栏推荐
- One question per day (2022-07-02) - Minimum refueling times
- 性能优化之关键渲染路径
- 更安全、更智能、更精致,长安Lumin完虐宏光MINI EV?
- Scala basic tutorial -- 14 -- implicit conversion
- The 15th youth informatics competition in Shushan District in 2019
- 数组中的第K个最大元素
- Shell 编程核心技术《二》
- LeetCode FizzBuzz C#解答
- 删除字符串中出现次数最少的字符【JS,Map排序,正则】
- Scala basic tutorial -- 19 -- actor
猜你喜欢

Torchdrug tutorial

MXNet对GoogLeNet的实现(并行连结网络)

物联网应用技术的就业前景和现状

神经网络物联网应用技术学什么

BI技巧丨权限轴

Scala基础教程--19--Actor

Wireshark packet capturing TLS protocol bar displays version inconsistency

Scala basic tutorial -- 19 -- actor

2022CoCa: Contrastive Captioners are Image-Text Fountion Models

Scala basic tutorial -- 13 -- advanced function
随机推荐
6.26cf simulation match B: solution to array reduction problem
Build your own website (15)
Nature microbiology | viral genomes in six deep-sea sediments that can infect Archaea asgardii
Bi skills - permission axis
大div中有多个div,这些div在同一行显示,溢出后产生滚动条而不换行
Scala基础教程--16--泛型
Using FTP
Other InterSystems%net tools
Li Chi's work and life summary in June 2022
Go microservice (II) - detailed introduction to protobuf
2014 Hefei 31st youth informatics Olympic Games (primary school group) test questions
Scala basic tutorial -- 15 -- recursion
工厂从自动化到数字孪生,图扑能干什么?
DeFi生态NFT流动性挖矿系统开发搭建
.NET ORM框架HiSql实战-第二章-使用Hisql实现菜单管理(增删改查)
Oracle with as ORA-00903: invalid table name 多表报错
神经网络物联网应用技术就业前景【欢迎补充】
Don't just learn Oracle and MySQL!
Unity adds a function case similar to editor extension to its script, the use of ContextMenu
读写关闭的channel是啥后果?