当前位置:网站首页>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 .
边栏推荐
- The difference and usage between substr (), slice (), and substring () in the string interception methods of "understand series after reading"
- OpenCV的二值化处理函数threshold()详解
- 2022年字节跳动日常实习面经(抖音)
- Angry bird design based on unity
- Principle and application of ThreadLocal
- Scala基础教程--16--泛型
- 测试工程师如何“攻城”(上)
- 1672. Total assets of the richest customers
- Send and receive IBM WebSphere MQ messages
- 用实际例子详细探究OpenCV的轮廓绘制函数drawContours()
猜你喜欢
Angry bird design based on unity
Torchdrug tutorial
The latest progress of Intel Integrated Optoelectronics Research promotes the progress of CO packaging optics and optical interconnection technology
Mxnet implementation of googlenet (parallel connection network)
使用canal配合rocketmq监听mysql的binlog日志
MXNet对GoogLeNet的实现(并行连结网络)
A method of using tree LSTM reinforcement learning for connection sequence selection
英特尔集成光电研究最新进展推动共封装光学和光互连技术进步
One question per day (2022-07-02) - Minimum refueling times
千万不要只学 Oracle、MySQL!
随机推荐
2021 Hefei informatics competition primary school group
从实时应用角度谈通信总线仲裁机制和网络流控
Build your own website (15)
测试工程师如何“攻城”(下)
The difference and usage between substr (), slice (), and substring () in the string interception methods of "understand series after reading"
Torchdrug tutorial
Caché WebSocket
Download the first Tencent technology open day course essence!
Scala basic tutorial -- 19 -- actor
Scala basic tutorial -- 12 -- Reading and writing data
奥迪AUDI EDI INVOIC发票报文详解
Unity编辑器扩展C#遍历文件夹以及子目录下的所有图片
Pb extended DLL development (super chapter) (VII)
Cache é JSON uses JSON adapters
Guys, for help, I use MySQL CDC 2.2.1 (Flink 1.14.5) to write Kafka and set
Scala basic tutorial -- 18 -- set (2)
正则替换【JS,正则表达式】
IBM WebSphere MQ检索邮件
repeat_P1002 [NOIP2002 普及组] 过河卒_dp
2022CoCa: Contrastive Captioners are Image-Text Fountion Models