我们经常会用到xml操作,如下介绍了js、sql、vb等对xml的操作。
JS创建xml对象
//创建对象 function getDataXML() { var objTds = $("TEXTAREA"); var count = objTds.length; var jsonData; var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); //创建根元素 var root = xmlDoc.createElement("DtCostRecollectOverView"); xmlDoc.appendChild(root); for (var i = 0; i < count; i++) { var chatRoom = xmlDoc.createElement(objTds[i].name); chatRoom.text = objTds[i].value; root.appendChild(chatRoom); } return xmlDoc.xml; }
sql解析xml
DECLARE @params xml DECLARE @customparams xml = null -- 0、解析输入参数 DECLARE @filter nvarchar (max) SET @filter = @params. value( '(/params/filter)[1]' , 'nvarchar(max)') -- 0.2、 (2=2) DECLARE @customfilter nvarchar (max) SET @customfilter = @params. value ( '(/params/customfilter)[1]', 'nvarchar(max)') IF @customfilter = N'' SET @customfilter = N'2=2' -- 0.3、每页显示记录数 DECLARE @pagesize int SET @pagesize = @params. value( '(/params/pagesize)[1]' , 'int') -- 0.4、页码 DECLARE @pagenum int SET @pagenum = @params. value( '(/params/pagenum)[1]' , 'int') -- 0.5、排序字段 DECLARE @sortcol nvarchar (100) SET @sortcol = @params. value( '(/params/sortcol)[1]' , 'nvarchar(100)') IF @sortcol IS NULL OR @sortcol = '' SET @sortcol = N' p_Provider.ProviderGUID' -- 注意:根据XML中的定义设置默认排序 !!! ELSE SET @sortcol = @sortcol + N' ,p_Provider.ProviderGUID' -- 注意:如果 XML中配置了实体主键,这里要拼接主键排序 -- 0.6、当前公司 DECLARE @buguid nvarchar (100) SET @buguid = @customparams. value ( '(/params/BUGUID)[1]', 'nvarchar(100)') -- 注意:变量大小写与vb代码一致 !!! -- 0.7、当前产品服务 Code DECLARE @productcode nvarchar (4000) SET @productcode = @params. value ( '(/params/customfilter2)[1]', 'nvarchar(4000)')
VB中创建XML
这里是原生的做法,具体使用可做相应的封装:
Private Function GetUpdateRoomXml() As String Dim xmlDOM As New XmlDocument Dim xmlGACode As XmlElement = xmlDOM.CreateElement("SAVEAREA") For Each dr As DataRow In dtUpdate_p_Room.Rows Dim xmlRow As XmlElement = xmlDOM.CreateElement("ITEMS") Dim xmlRoomGUID As XmlElement = xmlDOM.CreateElement("RoomGUID") Dim xmlGABldCode As XmlElement = xmlDOM.CreateElement("GABldCode") Dim xmlGAUnitCode As XmlElement = xmlDOM.CreateElement("GAUnitCode") Dim xmlGARoomCode As XmlElement = xmlDOM.CreateElement("GARoomCode") Dim xmlGACode0 As XmlElement = xmlDOM.CreateElement("GACode") Dim xmlRoomCode As XmlElement = xmlDOM.CreateElement("RoomCode") xmlRoomGUID.InnerText = dr.Item("RoomGUID").ToString() xmlGABldCode.InnerText = dr.Item("GABldCode").ToString() xmlGAUnitCode.InnerText = dr.Item("GAUnitCode").ToString() xmlGARoomCode.InnerText = dr.Item("GARoomCode").ToString() xmlGACode0.InnerText = dr.Item("GACode").ToString() xmlRoomCode.InnerText = dr.Item("RoomCode").ToString() xmlRow.AppendChild(xmlRoomGUID) xmlRow.AppendChild(xmlGABldCode) xmlRow.AppendChild(xmlGAUnitCode) xmlRow.AppendChild(xmlGARoomCode) xmlRow.AppendChild(xmlGACode0) xmlRow.AppendChild(xmlRoomCode) xmlGACode.AppendChild(xmlRow) Next Return xmlGACode.OuterXml End Function