[LIB] Xml Serialization/Deserialization

The place to discuss scripting and game modifications for X³: Terran Conflict and X³: Albion Prelude.

Moderators: Scripting / Modding Moderators, Moderators for English X Forum

Post Reply
User avatar
s9ilent
Posts: 2011
Joined: Wed, 29. Jun 05, 01:45
x4

[LIB] Xml Serialization/Deserialization

Post by s9ilent » Sat, 23. Oct 10, 00:10

As the title suggests, this is a small lib package used to serialize and deserialize XML data.

Download
http://www.members.optusnet.com.au/whyi ... ib_xml.zip


v1.3 24/10/2010
Modified the lib.explorer to also include " " around the attribute values

v1.2
Now can handle (by ignoring) <!.....> lines

v1.1
Removed some logbook debugs I had left in the script
Added quotes around attribute values (whoops)

Included are 3 main scripts (+misc helper scripts)
lib.xml.deserialize - To read an xml string
lib.xml.serialize - To Save an xmlobject to a logfile
lib.xml.explorer - To display an $xmlObject in a custom menu (for debugging)


If an error is encountered during deserialization, it will return a string with the error instead of the xmlObject

If an error is encountered during serialization, it will print the error and continue serializing. The error is always going to be in the XMLText section (As nodes are validated before entering them). This does not check if the attributes is an array (it will just not print any attributes, if you put junk in there)





Note, I've programmed it to be able to deserialize output from a c# program. Normal garbage in, garbage out rules apply.
-Can NOT handle multiple root nodes
-Ignores declaration opening node <?......> and ~comments <!....>
-Can NOT handle <!....> nodes, interprets this as a normal node (i really should be able to program around this... but I'm lazy. If someone asks for it I will reconsider
-


The x3 data structure I'm using is

$XmlObject = array[5]
0 = element name
1 = Array<Attribute>, Attributes
2 = Array<XmlObject>, Child nodes
3 = XmlObject/Null, Parent object or null
4 = String/Null, XmlText or null

$Attribute = array[2]
0 = String, Attribute Name
1 = String/Null, Attribute Value



Example Tfile Input

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
  <Language id="44">
    <page id="7745">
      <t id="7745">&lt;?xml version="1.0"?&gt;
&lt;GRAMMAR LANGID="409"&gt;
  &lt;RULE TOPLEVEL="ACTIVE" NAME="setupdisplay"&gt;
    &lt;p&gt;grammar&lt;/p&gt;
    &lt;p&gt;display&lt;/p&gt;
  &lt;/RULE&gt;
  &lt;RULE TOPLEVEL="ACTIVE" NAME="setup"&gt;
    &lt;p&gt;reload&lt;/p&gt;
    &lt;p&gt;grammar&lt;/p&gt;
  &lt;/RULE&gt;
  &lt;RULE TOPLEVEL="ACTIVE" NAME="remsvc"&gt;
    &lt;p&gt;computer remove and uninstall S V C&lt;/p&gt;
  &lt;/RULE&gt;
  &lt;RULE TOPLEVEL="ACTIVE" NAME="shiptarg"&gt;
    &lt;p&gt;target&lt;/p&gt;
    &lt;RULEREF NAME="generictarg" /&gt;
  &lt;/RULE&gt;
  &lt;RULE TOPLEVEL="ACTIVE" NAME="shipmissile"&gt;
    &lt;p&gt;fire&lt;/p&gt;
    &lt;RULEREF NAME="missiles" /&gt;
  &lt;/RULE&gt;
  &lt;RULE TOPLEVEL="ACTIVE" NAME="destarg"&gt;
    &lt;RULEREF NAME="cname" /&gt;
    &lt;p&gt;designate&lt;/p&gt;
    &lt;RULEREF NAME="shortident" /&gt;
    &lt;O&gt;
      &lt;RULEREF NAME="numbers" /&gt;
    &lt;/O&gt;
    &lt;RULEREF NAME="numbers" /&gt;
  &lt;/RULE&gt;
  &lt;RULE TOPLEVEL="ACTIVE" NAME="normcom"&gt;
    &lt;RULEREF NAME="shipid" /&gt;
    &lt;RULEREF NAME="commands" /&gt;
  &lt;/RULE&gt;
  &lt;RULE TOPLEVEL="ACTIVE" NAME="normcoma"&gt;
    &lt;RULEREF NAME="shipid" /&gt;
    &lt;RULEREF NAME="commandsa" /&gt;
  &lt;/RULE&gt;
  &lt;RULE TOPLEVEL="ACTIVE" NAME="normcomb"&gt;
    &lt;RULEREF NAME="shipid" /&gt;
    &lt;RULEREF NAME="commandsb" /&gt;
  &lt;/RULE&gt;
  &lt;RULE TOPLEVEL="ACTIVE" NAME="normcomc"&gt;
    &lt;RULEREF NAME="shipid" /&gt;
    &lt;RULEREF NAME="commandsc" /&gt;
  &lt;/RULE&gt;
&lt;/GRAMMAR&gt;</t>
    </page>
  </Language>
(What it looks like in normal xml)

Code: Select all

<GRAMMAR LANGID="409">
  <rule TOPLEVEL="ACTIVE" NAME="setupdisplay">
    <p>grammar</p>
    <p>display</p>
  </rule>
  <rule TOPLEVEL="ACTIVE" NAME="setup">
    <p>reload</p>
    <p>grammar</p>
  </rule>
  <rule TOPLEVEL="ACTIVE" NAME="remsvc">
    <p>computer remove and uninstall S V C</p>
  </rule>
  <rule TOPLEVEL="ACTIVE" NAME="shiptarg">
    <p>target</p>
    <RULEREF NAME="generictarg" />
  </rule>
  <rule TOPLEVEL="ACTIVE" NAME="shipmissile">
    <p>fire</p>
    <RULEREF NAME="missiles" />
  </rule>
...

Example Output logfile

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
 <GRAMMAR LANGID="409">
  <RULE TOPLEVEL="ACTIVE" NAME="setupdisplay">
   <p>
    grammar
   </p>
   <p>
    display
   </p>
  </RULE>
  <RULE TOPLEVEL="ACTIVE" NAME="setup">
   <p>
    reload
   </p>
   <p>
    grammar
   </p>
  </RULE>
  <RULE TOPLEVEL="ACTIVE" NAME="remsvc">
   <p>
    computer remove and uninstall S V C
   </p>
  </RULE>
  <RULE TOPLEVEL="ACTIVE" NAME="shiptarg">
   <p>
    target
   </p>
   <RULEREF NAME="generictarg" />
  </RULE>
  <RULE TOPLEVEL="ACTIVE" NAME="shipmissile">
   <p>
    fire
   </p>
   <RULEREF NAME="missiles" />
  </RULE>
  <RULE TOPLEVEL="ACTIVE" NAME="destarg">
   <RULEREF NAME="cname" />
   <p>
    designate
   </p>
   <RULEREF NAME="shortident" />
   <O>
    <RULEREF NAME="numbers" />
   </O>
   <RULEREF NAME="numbers" />
  </RULE>
  <RULE TOPLEVEL="ACTIVE" NAME="normcom">
   <RULEREF NAME="shipid" />
   <RULEREF NAME="commands" />
  </RULE>
  <RULE TOPLEVEL="ACTIVE" NAME="normcoma">
   <RULEREF NAME="shipid" />
   <RULEREF NAME="commandsa" />
  </RULE>
  <RULE TOPLEVEL="ACTIVE" NAME="normcomb">
   <RULEREF NAME="shipid" />
   <RULEREF NAME="commandsb" />
  </RULE>
  <RULE TOPLEVEL="ACTIVE" NAME="normcomc">
   <RULEREF NAME="shipid" />
   <RULEREF NAME="commandsc" />
  </RULE>
 </GRAMMAR>

Post Reply

Return to “X³: Terran Conflict / Albion Prelude - Scripts and Modding”