Tuesday, February 19, 2008

[Java How-To] #2 Writing XML Without a Plugin

*Update: Because of the inherent support for E4X in Rhino 1.6r1 and above, Servoy 4's support for XML is greatly improved. For more information, see the "XML" and "XMLList" nodes in the "JS Lib" within Servoy Developer's new  "Solution Explorer."

Java How-To's are intended to provide real-world examples of how Java can be used to easily extend Servoy in useful ways. I will try to post a new one as often as possible.


Description

If you thought you needed a plugin to write XML, think again. Because Servoy has such great support for Java, you can actually write XML documents without using a plugin, by simply embedding some Java code directly within your Servoy methods. The amount of code it takes to create an XML document complete with processing instructions, attributes and comments is minimal, and is shown below...

By the way...Servoy 4 (from what I have heard) introduces the ability to write XML documents without having to script any Java *or* use a plugin :-) To learn more about that, read up on Rhino (1.6 or greater).

Java Classes Used

For more information about any of the Java classes used, click their corresponding link below.

Total Lines of Code

Less than 15

The Servoy Code (Creating an XML Document)

var document_builder_factory = Packages.javax.xml.parsers.DocumentBuilderFactory.newInstance();

var document_builder = document_builder_factory.newDocumentBuilder();

// Create document
var doc = document_builder.newDocument(); // Doc is an object

// Create and append a processing instruction
var processing_instruction = doc.createProcessingInstruction("xml-stylesheet", "type=\"text/css\" href=\"http://www.domain.tld/style.css\" media=\"all\"");
doc.appendChild(processing_instruction);

// Create root element and append to document
var root_element = doc.createElement("root");
doc.appendChild(root_element);

// Insert a comment in front of the root element
var comment = doc.createComment("I am a comment");
doc.insertBefore(comment, root_element);

// Create a child element ("child1") of the root element
var child_element1 = doc.createElement("child1");
root_element.appendChild(child_element1);

// Create an attribute for child element1
child_element1.setAttribute("attr", "value");

// Add a text node to child_element1
child_element1.appendChild(doc.createTextNode("Child 1"));


The Servoy Code (Writing Your XML Document out to a String)

var transformer_factory = new Packages.javax.xml.transform.TransformerFactory.newInstance();

var transformer = transformer_factory.newTransformer();

var dom_source = new Packages.javax.xml.transform.dom.DOMSource(doc); // Pass in your document object here

var string_writer = new Packages.java.io.StringWriter();

var stream_result = new Packages.javax.xml.transform.stream.StreamResult(string_writer);

transformer.transform(dom_source, stream_result);

var xml_string = string_writer.toString(); // At this point you could use the Servoy XMLReader plugin to read your XML String.

application.output(xml_string);


In Summary

That's it! Writing XML without a plugin. Please post any questions or comments you may have to the blog.

IMPORTANT: Please be advised that you should always backup your Servoy solution before implementing or attempting any of the techniques described in this blog. None of the techniques described are guaranteed to work in any way shape or form, or for any specific purpose. Use them entirely at your own risk.

3 comments:

Benoit said...

Thanks for the heads up Jeff - it's always mind-boggling to see how there are many ways to accomplish the same thing with Servoy...

Your blog is an extremely useful source of information - required reading for all Servoy developers IMHO.

Thank you for sharing with the rest of the community!

Cheers,

Ben

================================================
Benoit R. Savignac, président
Consultation LOGIsoft Consulting inc.
---------------------------------------------------------------
Servoy Alliance Network Partner
---------------------------------------------------------------
49 rue Doucet, Saint-Basile-le-Grand, Québec J3N 1H9
T: 450.461.1626 ~ F: 450.461.1609
E: info@logisoft.qc.ca ~ W: www.logisoft.qc.ca

Tom Parry said...

Jeff, since the javax.xml.parsers.DocumentBuilderFactory.newinstance() is a static method you do not need the "new" in front of it. The return value is the reference to the instance.

Always check for the keyword "static" when examining the Java class it means that it does not need an instantiation to use it.

Good example though.
Tom Parry

Jeff Bader said...

Thanks for the tip Tom. I have edited the source per your comment.

Post a Comment