Attachment 'netXML.v1.0.txt'

Download

   1 :Namespace netXML
   2 
   3 ⍝ Utility namespace to help create, edit and query XML variables and files.
   4 ⍝ See http://aplwiki.com/netXML for more information and examples.
   5 
   6 ⍝  Version 1.0 August 2015, Pierre Gilbert
   7 
   8 
   9     (⎕IO ⎕ML ⎕WX)←1 3 3
  10 
  11     ∇ xml←name Element content;attributes
  12      ⍝ Program to build a ⎕XML element
  13      ⍝ content = content of the Xml element
  14      ⍝ name    = name of the Xml element and optionnally attribute(s)
  15 
  16      ⍝ Cases permitted:
  17      ⍝      'name' Element 'content'
  18      ⍝      'name' ('attribute1' 'value1') Element 'content'
  19      ⍝      'name' (('attribute1' 'value1')('attribute2' 'value2')) Element 'content'
  20      
  21      ⍝ Parse 'name'
  22       :If 1≠≡name
  23       :AndIf 2=⍴name
  24         ⍝ Nested with 2 elements
  25           attributes←2⊃name
  26           name←1⊃name
  27      
  28           :If 3=≡attributes
  29             ⍝ Attributes are separated like:
  30             ⍝ 'name' (('attribute1' 'value1')('attribute2' 'value2'))
  31               attributes←⊃attributes
  32      
  33           :ElseIf 2=≡attributes
  34             ⍝ Attributes are separated like:
  35             ⍝ 'name' ('attribute1' 'value1')
  36               attributes←((0.5×⍴attributes),2)⍴attributes
  37      
  38           :Else
  39               ⎕←'Don''t know what to do with the attribute(s) of name !'
  40               →0
  41      
  42           :End
  43      
  44       :ElseIf ' '=↑1↑0⍴∊name
  45         ⍝ element is not nested and is characters. No attributes.
  46           attributes←(0 2⍴⊂'')
  47      
  48       :Else
  49           ⎕←'Don''t know what to do with name !'
  50           →0
  51      
  52       :End
  53      
  54       xml←1 5⍴0 name(⍕content)attributes 0
  55 
  56 
  57     ∇ xml←names Elements contents
  58      ⍝ Using Element to iterate through each values of names and contents.
  59      
  60       xml←⊃⍪/names{⍺ Element ⍵}¨contents
  61 
  62 
  63     ∇ xml←Comment content
  64      ⍝ Program to build a ⎕XML comment element
  65      ⍝ content = content of the comment
  66      
  67       xml←1 5⍴0('!-- ',(⍕content),' --')''(0 2⍴⊂'')0
  68 
  69 
  70     ∇ xml←parentName AddParent children;attributes
  71      ⍝ Program to add a Parent element to children element(s)
  72      ⍝ children   = ⎕XML representation of the children
  73      ⍝ parentName = name of the parent with optionnally attribute(s)
  74      ⍝ xml        = ⎕XML representation of parent with children
  75      
  76      ⍝ Parse 'parentName'
  77       :If 1≠≡parentName
  78       :AndIf 2=⍴parentName
  79           attributes←2⊃parentName
  80           parentName←1⊃parentName
  81      
  82           :If 3=≡attributes
  83             ⍝ Attribute are separated like:
  84             ⍝ 'name' (('attribute1' 'value1')('attribute2' 'value2'))
  85               attributes←⊃attributes
  86      
  87           :ElseIf 2=≡attributes
  88             ⍝ Attributes are separated like:
  89             ⍝ 'name' ('attribute1' 'value1')
  90               attributes←((0.5×⍴attributes),2)⍴attributes
  91      
  92           :Else
  93               'Don''t know what to do with the attribute(s) of parentName !'
  94               →0
  95      
  96           :End
  97      
  98       :ElseIf ' '=↑1↑0⍴∊parentName
  99          ⍝ parentName is not nested and is characters. No attributes.
 100           attributes←(0 2⍴⊂'')
 101      
 102       :Else
 103           'Don''t know what to do with parentName !'
 104           →0
 105      
 106       :End
 107      
 108      ⍝ Parse 'children'
 109       :If 2=⍴⍴children  ⍝ Matrix ?
 110         ⍝ children is a matrix, parentName will wrap the elements of the matrix
 111           children[;1]+←1
 112           xml←(0 parentName''attributes 0)⍪children
 113      
 114       :Else
 115           'Don''t know what to do with children !'
 116           →0
 117      
 118       :EndIf
 119 
 120 
 121     ∇ xmlDoc←AplToXmlDoc apl;xml;⎕USING
 122      ⍝ Get a .Net XmlDocument from a ⎕Xml array.
 123      
 124      ⍝ Convert the ⎕XML array to characters.
 125       xml←(⎕XML⍠('Markup' 'Preserve'))apl   ⍝ Convert to Characters
 126       ((xml='¯')/xml)←'-'                   ⍝ Change the negative sign.
 127      
 128      ⍝ Get a .Net XmlDocument object from the characters.
 129       ⎕USING←'System.Xml,System.Xml.dll'
 130       xmlDoc←⎕NEW XmlDocument
 131       xmlDoc.LoadXml(⊂,xml)
 132 
 133 
 134     ∇ apl←XmlDocToApl xmlDoc
 135      ⍝ Get a ⎕XML array from a XmlDocument
 136      
 137       apl←(⎕XML⍠('Markup' 'Preserve'))xmlDoc.InnerXml
 138 
 139 
 140     ∇ xmlDoc AppendElement element;xmlDoc2;⎕USING;node
 141      ⍝ Append a ⎕XML array at the end of a XmlDocument
 142      ⍝ element = ⎕XML array
 143      ⍝ xmlDoc  = .Net XmlDocument
 144      
 145       :If 2=⍴⍴element
 146         ⍝ Convert array to characters
 147           element←⎕XML element
 148      
 149       :Else
 150           ⎕←'Don''t know what to do with element !'
 151           →0
 152      
 153       :End
 154      
 155      ⍝ Create a new XmlDocument with the element to Append
 156       ⎕USING←'System.Xml,System.Xml.dll'
 157       xmlDoc2←⎕NEW XmlDocument
 158       xmlDoc2.LoadXml(⊂,element)
 159      
 160      ⍝ Import and Append the new node.
 161       node←xmlDoc.ImportNode(xmlDoc2.DocumentElement,1)
 162       {}xmlDoc.DocumentElement.AppendChild(node)
 163 
 164 
 165     ∇ nodes←xmlDoc XPath query;⎕USING
 166      ⍝ Query a .Net XmlDocument with a XPath expression
 167      ⍝ Example for XPath can be found at: https://msdn.microsoft.com/en-us/library/ms256086%28v=vs.110%29.aspx
 168 
 169      ⍝ query  = Pattern search with syntax of XPath
 170      ⍝ xmlDoc = .Net XmlDocument Object
 171      
 172       nodes←xmlDoc.SelectNodes(⊂,query)
 173 
 174 
 175     ∇ r←apl AplToFile fileName;xml;xmlDoc;⎕USING
 176      ⍝ Save a ⎕XML array as characters to a file (Read it back with FileToApl).
 177      ⍝ fileName = fully qualified file name.
 178      ⍝ apl      = ⎕XML array
 179 
 180      ⍝ Success: 1⊃r = 1
 181 
 182      ⍝ Failure: 1⊃r = 0
 183      ⍝          2⊃r = Error description
 184      
 185      ⍝ Empty XmlDocument
 186       ⎕USING←'System.Xml,System.Xml.dll'
 187       xmlDoc←⎕NEW XmlDocument
 188      
 189      ⍝ Convert the ⎕XML array to characters and save it to file.
 190       :Trap 0
 191           xml←(⎕XML⍠('Markup' 'Preserve'))apl   ⍝ Convert to Characters
 192           ((xml='¯')/xml)←'-'                   ⍝ Change the negative sign.
 193      
 194           xmlDoc.LoadXml(⊂,xml)
 195           xmlDoc.Save(⊂,fileName)
 196           r←1
 197      
 198       :Else
 199          ⍝ Build error message
 200           :If 90=⎕EN
 201             ⍝ .Net Error
 202               r←0 ⎕EXCEPTION.Message
 203      
 204           :Else
 205             ⍝ APL Error
 206               r←0(1⊃⎕DM),': ',(2⊃⎕DM)
 207      
 208           :EndIf
 209       :End
 210 
 211 
 212     ∇ apl←FileToApl fileName;⎕USING;xmlDoc
 213      ⍝ Retrieve a ⎕XML array from a file name (saved with AplToFile).
 214      ⍝ fileName = fully qualified file name
 215 
 216      ⍝ Success: 1⊃apl = 1
 217      ⍝          2⊃apl = ⎕XML array
 218 
 219      ⍝ Failure: 1⊃apl = 0
 220      ⍝          2⊃apl = Error description
 221      
 222      ⍝ Empty XmlDocument
 223       ⎕USING←'System.Xml,System.Xml.dll'
 224       xmlDoc←⎕NEW XmlDocument
 225      
 226      ⍝ Read the file and convert to ⎕XML array
 227       :Trap 0
 228           xmlDoc.Load(⊂,fileName)
 229           apl←1((⎕XML⍠('Markup' 'Preserve'))xmlDoc.InnerXml)
 230      
 231       :Else
 232          ⍝ Build error message
 233           :If 90=⎕EN
 234             ⍝ .Net Error
 235               apl←0 ⎕EXCEPTION.Message
 236      
 237           :Else
 238             ⍝ APL Error
 239               apl←0(1⊃⎕DM),': ',(2⊃⎕DM)
 240      
 241           :EndIf
 242      
 243       :End
 244 
 245 
 246 :EndNamespace

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2015-08-21 16:26:11, 7.5 KB) [[attachment:netXML.v1.0.txt]]
  • [get | view] (2015-08-29 22:17:03, 8.4 KB) [[attachment:netXML.v1.1.txt]]
 All files | Selected Files: delete move to page

You are not allowed to attach a file to this page.