Date: 2004-09-15T20:46:37
Editor: TonyEdwards <[EMAIL PROTECTED]>
Wiki: Cocoon Wiki
Page: FlowBasedXMLEditor
URL: http://wiki.apache.org/cocoon/FlowBasedXMLEditor
no comment
New Page:
== Page title (cookbook approach, tutorial , ...) ==
- TARGET-AUDIENCE: '''*beginner*''' advanced expert[[BR]]
- COCOON-RELEASES: 2.1.4, 2.1.5[[BR]]
- DOCUMENT-STATUS: '''*draft*''' reviewed released[[BR]]
----
=== What you will get from this page ===
This page will hopefully give you an insight into generating and processing XML
documents from within Flow.
=== Your basic skills ===
Comfortable with Flowscript
=== Technical prerequisites ===
=== Links to other information sources ===
----
=== page metadata ===
- AUTHOR: Tony Edwards[[BR]]
- AUTHOR-CONTACT: anthonybedwards AT optusnet DOT com DOT au[[BR]]
- REVIEWED-BY:[[BR]]
- REVIEWER-CONTACT:[[BR]]
My first cocoon application was a simple xml editor written under version 2.0.4.
[[BR]]It utilised actions extensively and became a bit of a maintenance
nightmare. It basically allowed the user to create, edit and render into
different formats an xml document derived from a proprietory namespace. One of
my greatest reservations about this method was the heavy emphasis on Java as
I'm not much of a java programmer at all.
[[BR]]When 2.1.4 came along I though I'd port the application and jazz it up at
the same time. I managed to scrape up enough examples and snippets to come up
with a fairly usable solution.
[[BR]]Using flow to manipulate xml proved to be pretty straight forward,
although I'm not too sure as to the efficiency and optimisability of its
application. [[BR]]So far so good though.
[[BR]] First step was to create a new flowscript file and include the necessary
XML manipulation classes. I'm generally flying blind when it comes to importing
all these classes so there may be some redundancy.
[[BR]]importClass(org.apache.xpath.XPathAPI);
[[BR]]importClass(javax.xml.parsers.DocumentBuilderFactory);
[[BR]]importClass(org.w3c.dom.Node);
[[BR]]importClass(org.w3c.dom.Element);
[[BR]]importClass(org.w3c.dom.NodeList);
[[BR]]// Create a new document, given the name of its root element
[[BR]]function newDocument(root, attributeName, attributeVal) {
[[BR]] var result =
DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
[[BR]] // Create a processing instruction targeted for xml.
[[BR]] var node = result.createProcessingInstruction("xml", "version='1.0'");
[[BR]] result.appendChild(node);
[[BR]] node = null;
[[BR]]
[[BR]] // Create a comment for the document.
[[BR]] node = result.createComment(
[[BR]] "sample xml file created using XML DOM object.");
[[BR]] result.appendChild(node);
[[BR]] node=null;
[[BR]]
[[BR]] result.appendChild(result.createElement(root));
[[BR]] print("Add attributes to root..." + attributeName);
[[BR]] if (attributeVal != null){
[[BR]] try {
[[BR]] print("creating attribute for root: " + attributeName);
[[BR]] var root = result.getFirstChild();
[[BR]] root.setAttribute(attributeName, attributeVal);
[[BR]]
[[BR]] } catch (error) {
[[BR]] cocoon.log.error("Could not add attribute node to root: " +
error);
[[BR]] }
[[BR]]
[[BR]] }
[[BR]]
[[BR]] return result;
[[BR]]}