Reading and Writing XML Files

Hello all,

Can anyone point me to some sample code for reading and writing XML files? Using Xojo 2024R1.1 and later.

Thank you,
Tim

I did find a sample for reading and will review that.

Thank you,
Tim

1 Like

Here is a sample for writing XML. This is an XJDF Submit Queue Entry. This XML is usually sent to a Server via a POST. The Code below lives in a class class called “CreateJMF_SubmitQueueEntry” Otherwise you can save the retuned xjmftext as an XML file.

Public Function SubmitQueueEntryXJMF() As String
  Var xjmfdoc As New XMLDocument
  xjmfdoc.PreserveWhitespace = True
  
  Var root As XMLNode
  root = xjmfdoc.AppendChild(xjmfdoc.CreateElement("XJMF"))
  root.SetAttribute("xmlns","http://www.CIP4.org/JDFSchema_2_0")
  IndentNode(root,0)
  
   Var Header As XMLNode
   Header = root.AppendChild(xjmfdoc.CreateElement("Header"))
   Header.SetAttribute("DeviceID","PrinectUpdateXJDF")
   Header.SetAttribute("Time",StartDateTime)
   IndentNode(Header,1)
  
   Var CommandSubmitQueueEntry As XMLNode
   CommandSubmitQueueEntry = root.AppendChild(xjmfdoc.CreateElement("CommandSubmitQueueEntry"))
   IndentNode(CommandSubmitQueueEntry,1)
  
    Header = CommandSubmitQueueEntry.AppendChild(xjmfdoc.CreateElement("Header"))
    Header.SetAttribute("DeviceID", "Send Prinect XJDF Updater")
    Header.SetAttribute("Time",StartDateTime)
    IndentNode(Header,2)
  
    Var QueueSubmissionParams As XMLNode
    QueueSubmissionParams = CommandSubmitQueueEntry.AppendChild(xjmfdoc.CreateElement("QueueSubmissionParams"))
    QueueSubmissionParams.SetAttribute("URL", JDFfile)
    IndentNode(QueueSubmissionParams,2)
  
   IndentNodeCloseTag(CommandSubmitQueueEntry,1)
  IndentNodeCloseTag(root,0)
  
  Var xjmftext As String = xjmfdoc.ToString
  Return xjmftext

End Function

There are two other methods and a constant I use for the indenting:

Private Const kIndent as String =     
Private Sub IndentNode(node As XmlNode, level As Integer)
  Var i As Integer
  Var s As String
  s = EndOfLine
  For i = 1 To level
    s = s + kIndent
  Next
  node.Parent.Insert(node.OwnerDocument.CreateTextNode(s),node)
End Sub
Private Sub IndentNodeCloseTag(node As XmlNode, level As Integer)
  Var i As Integer
  Var s As String
  s = EndOfLine
  For i = 1 To level
    s = s + kIndent
  Next
  node.AppendChild(node.OwnerDocument.CreateTextNode(s))
  
End Sub

Thank you Tom!

Tim

1 Like