<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>XML &#8211; Xojo Programming Blog</title>
	<atom:link href="https://blog.xojo.com/tag/xml/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.xojo.com</link>
	<description>Blog about the Xojo programming language and IDE</description>
	<lastBuildDate>Tue, 02 Mar 2021 17:53:06 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Working with XML</title>
		<link>https://blog.xojo.com/2019/02/21/working-with-xml/</link>
		
		<dc:creator><![CDATA[Paul Lefebvre]]></dc:creator>
		<pubDate>Thu, 21 Feb 2019 23:46:30 +0000</pubDate>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[Xojo Programming Language]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=5444</guid>

					<description><![CDATA[XML can sometimes be a bit confusing, so here are some tips to help you work with XML files. First, you need to make sure you XML is really XML.]]></description>
										<content:encoded><![CDATA[<p>XML can sometimes be a bit confusing, so here are some tips to help you work with XML files.</p>
<p>First, you need to make sure your XML is really XML.</p>
<p><span id="more-5444"></span></p>
<p>It needs to look like this (the ?xml prefix at the beginning is necessary):</p>
<pre>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;Ride&gt;
  &lt;VehicleInfo&gt;
    &lt;VehicleMake&gt;Tesla&lt;/VehicleMake&gt;
    &lt;VehicleModel&gt;S&lt;/VehicleModel&gt;
    &lt;VIN&gt;862462032347916&lt;/VIN&gt;
    &lt;ModelYear&gt;2018&lt;/ModelYear&gt;
    &lt;YearPurchased&gt;2017&lt;/YearPurchased&gt;
  &lt;/VehicleInfo&gt;
&lt;/Ride&gt;</pre>
<p>In a Xojo project, I put the above in a constant called kXML.</p>
<p>Now you can parse it to get the XmlNode that points to what you want to change. One way is to directly refer to it like this:</p>
<pre>Dim xml As New XmlDocument
xml.LoadXml(kXML)

// Get &lt;VehicleInfo&gt;
Dim vehicleInfo As XmlNode
// xml.Firstchild is &lt;Ride&gt; and its FirstChild is &lt;VehicleInfo&gt;
vehicleInfo = xml.FirstChild.FirstChild

// Get &lt;VehicleModel&gt;
// This is the 2nd (0-based so use 1) child of &lt;VehicleInfo&gt;
Dim vehicleModel As XmlNode
vehicleModel = vehicleInfo.Child(1)

// Get value for &lt;VehicleModel&gt;'s child which is "S"
Dim vehicleModelValue As String = vehicleModel.FirstChild.Value

// Change value for &lt;VehicleModel&gt;'s child
vehicleModel.FirstChild.Value = "3"

// The updated XML which you can use, save, etc.
Dim newXML As String = xml.ToString</pre>
<p>As an alternative to the above you could search directly for the node you want and then change it. You would do that using Xql:</p>
<pre>Dim xml As New XmlDocument
xml.LoadXml(kXML)

// Search entire XML document for anything that contains &lt;VehicleModel&gt; and
// get the results back as a list.
Dim vehicleModels As XmlNodeList
vehicleModels = xml.Xql("//VehicleModel")

If vehicleModels.Length &gt;= 0 Then
  // At least one &lt;VehicleModel&gt; was found so change the first one's value
  // Change value for &lt;VehicleModel&gt;
  vehicleModels.Item(0).FirstChild.Value = "3"
End If

Dim newXML As String = xml.ToString</pre>
<p>For reference:</p>
<ul>
<li><a href="https://documentation.xojo.com/api/text/xml/xmldocument.html">XMLDocument</a> class</li>
<li><a href="https://documentation.xojo.com/api/text/xml/xmlnode.html">XMLNode</a> class</li>
<li><a href="https://documentation.xojo.com/api/text/xml/xmlnode.htmlList">XMLNodeList</a> class</li>
<li><a href="https://documentation.xojo.com/topics/file_managment/reading_and_writing_data_in_xml_format.html">User Guide: XML Documents</a> topic</li>
</ul>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Formatting Your XML</title>
		<link>https://blog.xojo.com/2018/08/21/formatting-your-xml/</link>
		
		<dc:creator><![CDATA[Paul Lefebvre]]></dc:creator>
		<pubDate>Tue, 21 Aug 2018 10:13:01 +0000</pubDate>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[XML]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=4841</guid>

					<description><![CDATA[Although Xojo does not have a built-in method to format XML text, you can use XSLT to do this for you. To use this with Xojo, add a module to your project (name it XMLExtensions), add a String constant to the module (call it kXSLTFormat) and copy the above XSLT into the constant.]]></description>
										<content:encoded><![CDATA[<p>Although Xojo does not have a built-in method to format XML text, you can use XSLT to do this for you. XSLT stands for eXtensible Stylesheet Language. This XSLT can be used to format XML:</p>
<pre>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
&lt;xsl:output method="xml" indent="yes" /&gt;
&lt;xsl:template match="/"&gt;
&lt;xsl:copy-of select="/" /&gt;
&lt;/xsl:template&gt;
&lt;/xsl:transform&gt;</pre>
<p>To use this with Xojo, add a module to your project (name it <strong>XMLExtensions</strong>), add a String constant to the module (call it <strong>kXSLTFormat</strong>) and copy the above XSLT into the constant.</p>
<p><span id="more-4841"></span></p>
<p>Now add a Global function to the module that <a href="https://blog.xojo.com/2013/09/13/using-extension-methods/">extends</a> the XMLDocument class with a Format function:</p>
<pre>Function Format(Extends xml As XMLDocument) As String
  Return xml.Transform(kXSLTFormat)
End Function</pre>
<p>Now you can call it to get back formatted XML as a String. For example, this code takes unformatted XML from a TextField and displays the formatted XML in another TextField:</p>
<pre>Dim xml As New XMLDocument
xml.LoadXml(TextArea1.Text)
TextArea2.Text = xml.Format</pre>
<p><img fetchpriority="high" decoding="async" class="size-full wp-image-4842 aligncenter" src="https://blog.xojo.com/wp-content/uploads/2018/08/2018-08-17_15-45-28.png" alt="" width="605" height="577" /></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Is There a Correct Structure for Your XML?</title>
		<link>https://blog.xojo.com/2017/10/20/is-there-a-correct-structure-for-your-xml/</link>
		
		<dc:creator><![CDATA[Paul Lefebvre]]></dc:creator>
		<pubDate>Fri, 20 Oct 2017 07:15:07 +0000</pubDate>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[XML]]></category>
		<guid isPermaLink="false">http://blog.xojo.com/?p=2864</guid>

					<description><![CDATA[Xojo makes it possible to structure the XML any way you want. There really is no right or wrong XML format as both of these contain the same information. Sometimes it just comes down to preference or requirements from other software that maybe be using the XML. ]]></description>
										<content:encoded><![CDATA[<p>I recently had a customer ask about how to adjust XML structure and whether one format is better than another.</p>
<p>They had XML in this format:</p>
<pre>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;xmldata&gt;
  &lt;row username="Mary" message="Welcome!" disable="NO"/&gt;
&lt;/xmldata&gt;</pre>
<p>&nbsp;</p>
<p>That XML was generated using code like this:</p>
<p><span id="more-2864"></span></p>
<pre>Dim xml As New XmlDocument

Dim root As XmlNode
root = xml.AppendChild(xml.CreateElement("xmldata"))

Dim row As XmlNode
row = root.AppendChild(xml.CreateElement("row"))
row.SetAttribute("username", "Mary")

Dim message As String = "Welcome!"
row.SetAttribute("message", message)

row.SetAttribute("disable", "NO")

Dim xmlText As String = xml.ToString</pre>
<p>But because of a dependency on something else that used the XML they really wanted the XML to instead look like this:</p>
<pre>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;xmldata&gt;
  &lt;username&gt;Mary&lt;/username&gt;
  &lt;message&gt;Welcome!&lt;/message&gt;
  &lt;disable&gt;NO&lt;/disable&gt;
&lt;/xmldata&gt;</pre>
<p>Since XML is always tricky, they were understandably confused about how to create the XML structure they needed. The trick here is that rather than creating the values as XML attributes they instead needed to be created as XMLTextNodes. Here is the code:</p>
<pre>Dim xml As New XmlDocument

Dim root As XmlNode
root = xml.AppendChild(xml.CreateElement("xmldata"))

Dim snNode As XmlNode = root.AppendChild(xml.CreateElement("name"))
Dim snValue As XmlTextNode = xml.CreateTextNode("")
snValue.Value = "Mary"
snNode.AppendChild(snValue)

Dim message As String = "Welcome!"
Dim msgNode As XmlNode = root.AppendChild(xml.CreateElement("message"))
Dim msgValue As XmlTextNode = xml.CreateTextNode("")
msgValue.Value = message
msgNode.AppendChild(msgValue)

Dim disableNode As XmlNode = root.AppendChild(xml.CreateElement("disable"))
Dim disableValue As XmlTextNode = xml.CreateTextNode("")
disableValue.Value = "NO"
disableNode.AppendChild(disableValue)

Dim xmlText As String = xml.ToString</pre>
<p>There really is no right or wrong XML format as both of these contain the same information. Sometimes it just comes down to preference or requirements from other software that may be using the XML. Fortunately, <a href="http://developer.xojo.com/userguide/xml">Xojo makes it possible</a> to structure the XML any way you want.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
