Hi,

I used gnosis to serialize jython object to xml.
Following is the class:

from net.grinder.script.Grinder import grinder

class person(object):
def __init__(self):
self._name = None

def getname(self):
return self._name
def setname(self,value):
self._name = value
name = property(getname, setname, "viji")

code is as follows:

from net.grinder.script.Grinder import grinder
from net.grinder.script import Test
from net.grinder.plugin.http import HTTPRequest
import java
import java.io.FileOutputStream
from java.lang import *
from personclass import person
import gnosis
from gnosis.xml.pickle import XML_Pickler

test1 = Test(1, "Test1")
request1 = test1.wrap(HTTPRequest())

class TestRunner:
def __call__(self):
grinder.statistics.delayReports=True
grinder.logger.info("vij Hello World 1")
p1 = person()
p1.name = "viji"
xmlstring = XML_Pickler().dumps(p1)
# p2 = XML_Pickler().loads(xmlstring)
grinder.logger.info(" vij xml string " + str(xmlstring))

But I receive following output:

<?xml version="1.0"?>
<!DOCTYPE PyObject SYSTEM "PyObjects.dtd">
<PyObject module="personclass" class="person" id="2">
<attr name="_name" type="string" value="viji" />
</PyObject>

Whereas, I want the xml to look the one as follows:

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<person xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">
 <name>viji</name>
</person>

What is missing in the code? How can I get xml in the format I need?


On Wed, Mar 6, 2013 at 4:59 AM, Alan Kennedy <jython-dev@xhaus.com> wrote:
[vijetha]

> I use Jython-2.5.2 for performance testing in Grinder 3.11
> How can I serialize an object to xml in Jython? What library files are needed to be added?

This sweb page has some promising approaches, which should workin jython.

serializing python objects to XML
http://stackoverflow.com/questions/2101902/serializing-python-objects-to-xml

In particular, the gnosisXML package should work.

You could also consider serialising to JSON

http://jyson.xhaus.com

Then converting to XML. Although I would not bother with the XML step, I would probably just leave it in JSON.

Unless you have a specific XML related requirement.

Alan.