EnglishGermanFrenchRussianItalianSpanish
Log inRegister
 
[LIB] Xml Serialization/Deserialization
Post new topic Reply to topic
View previous topic :: View next topic
Author Message
s9ilent





Joined: 29 Jun 2005
Posts: 2009 on topic
Location: Galactic Sector ZZ9-Plural Z alpha +10 GMT
Thank you for registering your game
PostPosted: Sat, 23. Oct 10, 00:10    Post subject: [LIB] Xml Serialization/Deserialization Reply with quote Print

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

Download
http://www.members.optusnet.com.au/whyisthat/scripts/lib_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:
<?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:
<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:
<?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>


Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Display posts from previous:   
Post new topic Reply to topic
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum
Control Panel
Login Data
The time now is Mon, 11. Dec 17, 13:34

All times are GMT + 2 Hours


Board Security

Copyright © EGOSOFT 1989-2017
Powered by phpBB © 2001, 2005 phpBB Group
Template created by Avatar & BurnIt!
Debug: page generation = 0.07400 seconds, sql queries = 15