How to store a whole XML document as value in DB

Questions and answers on designing your Servoy solutions, database modelling and other 'how do I do this' that don't fit in any of the other categories

How to store a whole XML document as value in DB

Postby 258.d.852 » Fri Apr 12, 2024 10:05 am

Hi community,

In my Postgresql table, there is a column has the type of xml and I would like to persist the whole XML document there.

In Servoy app, I can read the content of XML document via a plugins plugins.XmlReader.readXmlDocumentFromFile. The returned value is an array of XmlNode.

At my first try, I simply assign the returned value to the column of the table via the foundset.

Code: Select all
    var xmlsFS = datasources.db.mydatabase.my_table.getFoundSet();
    xmlsFS.newRecord();
    xmlsFS.xml_content = xmlNodes[0];
    databaseManager.saveData(xmlsFS);


It throws an error

ERROR com.servoy.j2db.util.Debug - Couldn't convert: Node[name:catalog, type: ELEMENT] to media data


How can I overcome this issue?

Thanks
258.d.852
 
Posts: 10
Joined: Wed Jul 12, 2023 1:10 pm

Re: How to store a whole XML document as value in DB

Postby mboegem » Fri Apr 12, 2024 10:35 am

Hi,

your trying write an javascript object to the database into a media type column.

Basically 2 ways to overcome this:

1) convert XML to a string and store it in a text column (create new column of type text with length '-1')
2) convert XML to a string, convert it to a byteArray: utils.stringToBytes(xmlString) and store it int the xml_content column you have right now.

Advantage of using option 1 is that the xml ends up as readable text in your database.

Hope this helps
Marc Boegem
Solutiative / JBS Group, Partner
Servoy Specialist
• Servoy Certified Developer
• Servoy Valued Professional
• Freelance Developer

Image
User avatar
mboegem
 
Posts: 1752
Joined: Sun Oct 14, 2007 1:34 pm
Location: Amsterdam


Return to Programming with Servoy

Who is online

Users browsing this forum: No registered users and 8 guests