附: IRIS XML使用示例

util.XMLUtils

Class util.XMLUtils Extends (%RegisteredObject, %XML.Adaptor)
{

/// %XML.Reader
/// d ##class(util.XMLUtils).XMLWrite()
ClassMethod XMLWrite()
{
	s obj = ##class(MyApp.Person).%OpenId(1)
	
	//1. 对象写入XML输出
	
	// 创建实例
	s writer = ##class(%XML.Writer).%New()
	s writer.Indent = 1 //设置缩进
	
	// 读取对象
	#;	d writer.OutputToString()
	#;	d writer.RootObject(obj)
	#;	s xml = writer.GetXMLString()
	#;	w xml ,!
	s status = writer.OutputToStream(.stream)
	i $$$ISERR(status) {d $System.Status.DisplayError(status) q}
	s status = writer.RootObject(obj)
	i $$$ISERR(status) {d $System.Status.DisplayError(status) q}
	w stream.Read() ,!
	
	//2. 手动编写XML输出
	set xml=##class(%XML.Writer).%New()
	s xml.Indent = 1
	d xml.OutputToStream(.stream)
	d xml.RootElement("Person")
		d xml.Element("IDCard"), xml.Write("340124202003054873") ,xml.EndElement()
		d xml.Element("Name"), xml.Write("wansk") ,xml.EndElement()
		d xml.Element("Relationships")
			d xml.Element("Relationship")
				d xml.Element("Relation"), xml.Write("father") ,xml.EndElement()
				d xml.Element("Name"), xml.Write("wanyh") ,xml.EndElement()
			d xml.EndElement()
			d xml.Element("Relationship")
				d xml.Element("Relation"), xml.Write("mother") ,xml.EndElement()
				d xml.Element("Name"), xml.Write("lumina") ,xml.EndElement()
			d xml.EndElement()
		d xml.EndElement()
	d xml.EndRootElement()
	w stream.Read() ,!
	

	#;-------------------------------------------
	#;	<?xml version="1.0" encoding="UTF-8"?>	-
	#;	<Person>								-
	#;	  <IDCard>340124202003054873</IDCard>	-
	#;	  <Name>wansk</Name>					-
	#;	  <Relationships>						-
	#;	    <Relationship>						-
	#;	      <Relation>father</Relation>		-
	#;	      <Name>wanyh</Name>				-
	#;	    </Relationship>						-
	#;	    <Relationship>						-
	#;	      <Relation>mother</Relation>		-
	#;	      <Name>lumina</Name>				-
	#;	    </Relationship>						-
	#;	  </Relationships>						-
	#;	</Person>								-
	#;-------------------------------------------
}

/// %XML.Reader
/// d ##class(util.XMLUtils).XMLReader()
ClassMethod XMLReader()
{
	s str =			"<Person>"
	s str =	str _		"<IDCard>340124202003054873</IDCard>"			
	s str =	str _		"<Name>wansk</Name>"
    s str =	str _		"<Relationships>"
    s str =	str _			"<Relationship>"
    s str =	str _				"<Relation>father</Relation>"
    s str =	str _				"<Name>wanyh</Name>"
    s str =	str _			"</Relationship>"
    s str =	str _			"<Relationship>"
    s str =	str _				"<Relation>mother</Relation>"
    s str =	str _				"<Name>lumina</Name>"
    s str =	str _			"</Relationship>"
    s str =	str _		"</Relationships>"
	s str =	str _	"</Person>"
	
	#; 1. XMLReader
	// 创建实例,导入XML
	s reader = ##class(%XML.Reader).%New()
	s status = reader.OpenString(str)
	#; status = reader.OpenStream(Stream)
	i $$$ISERR(status) {d $System.Status.DisplayError(status) q}
	
	// 将XML导入对象
	d reader.CorrelateRoot("MyApp.Person")
	d reader.Next(.object, .status)
	i $$$ISERR(status) {d $System.Status.DisplayError(status) q}
	
	w "IDCard:", object.IDCard ,!
	w "Name:",  object.Name ,!
	w "RelationshipCount:",  object.Relationships.Count() ,!
	for i = 1 : 1 :object.Relationships.Count() {
		w "Relation:"_object.Relationships.GetAt(i).Relation ,!
		w "Name:"_object.Relationships.GetAt(i).Name ,!
	}
	
	
	#; 2. %XML.XPATH.Document
	s tSC = ##class(%XML.XPATH.Document).CreateFromString(str,.tDocument)
	#; s tSC = ##class(%XML.XPATH.Document).CreateFromStream(Input,.tDocument)
	i $$$ISERR(tSC) {d $System.Status.DisplayError(tSC) q}
	set tSC = tDocument.EvaluateExpression("/Person/IDCard","text()",.tRes)
	if ($$$ISOK(tSC) && (tRes.GetAt(1) '= "")){    
    	s fieldValue=tRes.GetAt(1).Value
    	s IDCard=$tr(fieldValue,$c(0),"")
    	w "IDCard:", IDCard ,!
   	}
   	set tSC = tDocument.EvaluateExpression("/Person/Name","text()",.tRes)
	if ($$$ISOK(tSC) && (tRes.GetAt(1) '= "")){    
    	s fieldValue=tRes.GetAt(1).Value
    	s Name=$tr(fieldValue,$c(0),"")
    	w "Name:", Name ,!
   	}
   	
   	set tSC=tDocument.EvaluateExpression("/Person/Relationships","count(Relationship)",.tRes)
   	if ($$$ISOK(tSC)&&(tRes.GetAt(1)'="")){
 		set hsCount=tRes.GetAt(1).Value
 		w "RelationshipCount:", hsCount ,!
		for i=1:1:hsCount {
			set tSC=tDocument.EvaluateExpression("/Person/Relationships/Relationship["_i_"]/Relation","text()",.tRes)                    
			if ($$$ISOK(tSC)&&(tRes.GetAt(1)'="")){    
         		set fieldValue=tRes.GetAt(1).Value
         		set Relation=$tr(fieldValue,$c(0),"")
      			w "Relation:", Relation ,!
       		} 
      		set tSC=tDocument.EvaluateExpression("/Person/Relationships/Relationship["_i_"]/Name","text()",.tRes)                    
    		if ($$$ISOK(tSC)&&(tRes.GetAt(1)'="")){    
        		set fieldValue=tRes.GetAt(1).Value
      			set Name=$tr(fieldValue,$c(0),"")
            	w "Name:", Name ,!
        	} 
    	}
   	}
   	
   	#;-------------------------------
	#;	IDCard340124202003054873	-
	#;	Name:wansk					-
	#;	RelationshipCount2		-
	#;	Relation:father				-
	#;	Name:wanyh					-
	#;	Relation:mother				-
	#;	Name:lumina					-
	#;-------------------------------
}

}

MyApp.Person MyApp.Relationship

Class MyApp.Person Extends (%Persistent, %XML.Adaptor, %JSON.Adaptor) [ SqlTableName = MyAppPerson ]
{

Parameter XMLNAME = "Person";

Index IDCardKey On IDCard [ Unique ];

/// Person's IDCard number.
Property IDCard As %String(%JSONFIELDNAME = "IDNum") [ Required ];

/// Name of the person.
Property Name As %String;

/// Person's Relationships.
Property Relationships As list Of MyApp.Relationship;

Storage Default
{
<Data name="PersonDefaultData">
<Value name="1">
<Value>%%CLASSNAME</Value>
</Value>
<Value name="2">
<Value>IDCard</Value>
</Value>
<Value name="3">
<Value>Name</Value>
</Value>
<Value name="4">
<Value>Relationships</Value>
</Value>
</Data>
<DataLocation>^MyApp.PersonD</DataLocation>
<DefaultData>PersonDefaultData</DefaultData>
<IdLocation>^MyApp.PersonD</IdLocation>
<IndexLocation>^MyApp.PersonI</IndexLocation>
<StreamLocation>^MyApp.PersonS</StreamLocation>
<Type>%Storage.Persistent</Type>
}

}

Class MyApp.Relationship Extends (%Persistent, %XML.Adaptor, %JSON.Adaptor) [ SqlTableName = Relationship ]
{

Parameter XMLNAME = "Relationship";

Property Relation As %String;

Property Name As %String;

Storage Default
{
<Data name="RelationshipDefaultData">
<Value name="1">
<Value>%%CLASSNAME</Value>
</Value>
<Value name="2">
<Value>Relation</Value>
</Value>
<Value name="3">
<Value>Name</Value>
</Value>
</Data>
<DataLocation>^MyApp.RelationshipD</DataLocation>
<DefaultData>RelationshipDefaultData</DefaultData>
<IdLocation>^MyApp.RelationshipD</IdLocation>
<IndexLocation>^MyApp.RelationshipI</IndexLocation>
<StreamLocation>^MyApp.RelationshipS</StreamLocation>
<Type>%Storage.Persistent</Type>
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wanyh2023

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值