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