User Tools

Site Tools


modding:function:xmlcreate

Discuss this page

xmlCreate

Syntax (xmlCreate xml) → xml
Arguments xml: The XML or string to use
Returns A new XML pointer
Category XML
Description Creates a new XML pointer from existing XML or converts a string into XML data. String must be properly formatted XML data.

XML data cannot be treated like a string despite appearing to be one when printed. However, it can be used to create a string using cat, though the result does not have the pointer.

XML Pointers

This function can be used to create a copy of an XML pointer variable. Consider the following code, where &unidHugoSandoval; is defined as shown below and its XML is then modified.

	<Type UNID="&unidHugoSandoval;">
		<StaticData>
			<Data id="MissionAttribute">"hugoSandovalMission"</Data>
			<Data id="Name">"Hugo Sandoval"</Data>
			<Data id="Sovereign">&svCommonwealth;</Data>
		</StaticData>

		<Events>
			<OnCharacterCanBeMet>
				True
			</OnCharacterCanBeMet>

			<OnGlobalSystemStopped>
				(rpgCharacterAscend &unidHugoSandoval;)
			</OnGlobalSystemStopped>
		</Events>
	</Type>

Here is the code that we will apply to this XML

	(block (xml1 xml2)						;Line 1
		(setq xml1 (typGetXML &unidHugoSandoval;))		;Line 2
		(xmlDeleteSubElement xml1 0)				;Line 3
		(setq xml2 (xmlCreate xml1))				;Line 4
		(xmlDeleteSubElement xml1 0)				;Line 5
		)

After Line 3, <StaticData> (the first subelement of &unidHugoSandoval;) has been removed and the value of xml1 looks like this:

	<Type UNID="&unidHugoSandoval;">
		<Events>
			<OnCharacterCanBeMet>
				True
			</OnCharacterCanBeMet>

			<OnGlobalSystemStopped>
				(rpgCharacterAscend &unidHugoSandoval;)
			</OnGlobalSystemStopped>
		</Events>
	</Type>

At Line 4, a new copy for the XML data in xml1 has been created and stored in xml2. The two variables have identical values. However, they are separate, so a change to xml1 will not affect the value of xml2. At Line 5, the <Events> element has been deleted from xml1, which now looks like this:

	<Type UNID="&unidHugoSandoval;">
	</Type>

While xml2 still looks like this:

	<Type UNID="&unidHugoSandoval;">
		<Events>
			<OnCharacterCanBeMet>
				True
			</OnCharacterCanBeMet>

			<OnGlobalSystemStopped>
				(rpgCharacterAscend &unidHugoSandoval;)
			</OnGlobalSystemStopped>
		</Events>
	</Type>

XML From Strings

The following code creates XML from a string and stores it in the variable theXML. Attempting to use xml-prefixed functions with a string will lead to an “invalid XML element” error. Note that the angle brackets < and > need to be escaped with &lt; and &gt; (respectively) when used outside of the in-game console or the TLisp Shell from TransData, as they are evaluated as part of the string. Furthermore, the quotation marks are also escaped as \“.

	(block (theXML)
		(setq theXML (xmlCreate "<Element attribute=\"5\"/>"))
		)

In file code, the working equivalent is this:

	(block (theXML)
		(setq theXML (xmlCreate "&lt;Element attribute=\"5\"/&gt;"))
		)

This sets the theXML to a valid XML data pointer that can be used with other functions.

The following code will not work:

	;Quotation marks have not been escaped, so this code will be interpreted incorrectly
	(xmlCreate "<Element attribute="5"/>")

	;XML element is not closed. Furthermore, it lacks content
	(xmlCreate "<Element attribute=\"5\">"))

	;XML element is closed, but it still lacks content
	(xmlCreate "<Element attribute=\"5\"></Element>")

	;XML element is closed, but it still lacks content
	(xmlCreate "<Element attribute=\"5\"></Element>")

	;Due to a typo, the end tag has a different name from the open tag
	(xmlCreate "<Element attribute=\"5\"></Elemenf>")

	;Additionally, this code will not work if used in an extension.
	(xmlCreate "<Element attribute=\"5\">")

The following examples will work:

	(xmlCreate "<Element/>")
	(xmlCreate "<Element>5</Element>")
	(xmlCreate "<Element>Text</Element>")
	(xmlCreate "<Element attribute=\"5\">Text</Element>")

XML

Return to XML Functions list

Return to Functions list

modding/function/xmlcreate.txt · Last modified: 2017/01/28 19:09 by 0xabcdef