Removing Hardcoding of XML elements from Struts action class - struts2

In my application I am using struts 2. we are sending an AJAX call using jQuery. I am formatting the XML data in Struts 2 action class and sending that as XML response to to AJAX request. I am formatting the XML data in struts actions similar to this:
<person>
<age>
</age>
<city>
</city>
</person>
What I am looking for is moving this XML data to a separate XML file. Someone told me that we can use Velocity to create XML dynamically. Can someone please help me? A sample code would be much appreciated. Thanks!

There are a number of options available to you. First, you can use JSP to output XML. To do so, just start the JSP off as follows:
<?xml version="1.0" encoding="UTF-8"?>
<%# page contentType="text/xml;charset=UTF-8" language="java" %>
Additionally, you can also use a templating language such as Velocity or Freemarker.
Lastly, for a bit of a different approach, you can use the XML Streaming API (javax.xml.stream package), which lets you programatically build XML.
If you are already using JSP for your view layer, then using JSP for your XML output may be the easiest approach. Otherwise, I'd recommend that you look at the streaming API. Velocity and Freemarker are both good templating languages, but I wouldn't personally use them just for producing XML, especially if you're using something else to generate your HTML.

Here is a quick snapshot how to init and use Velocity with Tools:
//init velocity
VelocityEngine velocity = new VelocityEngine();
velocity.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, "/path/to/templates/dir/");
velocity.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_CACHE, true);
velocity.init();
//init tools
ToolManager velocityToolManager = new ToolManager();
velocityToolManager.configure("velocity-tools.xml");
//get template
Template template = velocity.getTemplate("demo.vm"); //contains ${msg}
//create context
VelocityContext context = new VelocityContext(velocityToolManager.createContext());
//pass data do context
context.put("msg", "Hello from Velocity");
//process template
StringWriter sw = new StringWriter();
template.merge(context, sw);
//parsed template as a string
String parsedTemplate = sw.toString();

Related

Dhtmlxgrid 4.0 doesn't load data from the ASP.Net MVC view

In my ASP.Net MVC web application I used to have dhtmlxgrid plugin v 3.5 and now updated to v4.0.
Before it was easy to generate dynamic values for the grid in separate .cshtml view and only pass the url of the method inlo loadXML like this:
myGrid.loadXML('#Url.Action("XmlValues")');
Now in the version 4.0 they replaced loadXML() with load(), which still should be able to do the same thing. But instead of parsing the data into the grid it shows an alert with an xml code generated inside the view and the table body is empty. It can't even load simple xml if it was passes through the view:
<?xml version="1.0" encoding="UTF-8"?>
<rows>
<row id="1">
<cell>1</cell>
<cell>2</cell>
</row>
</rows>
But if I copy that into an external xml file and pass path to the file into the method it works correctly.
myGrid.load("/xml.xml");
What is the problem with new load() method and how to make it work with dynamic data in ASP.Net MVC as I used to?
please make sure there are no extra chars in the generated xml. For example, a space (" ") before the xml header.

How to use a gsp template to create a text file?

I have used the Grails Rendering Plugin in the past with much success in creating PDFs. Throw now I would like to create a simple text file, using a gsp. I loved the ease of using a model to define how to insert information into the template. I realize I don't need to render a text file, but is there a similar way to use a template to just create an ordinary text file?
Example from how to render a jpg using the Grails Render Plugin: (notice the model use)
def bytes = gifRenderingService.render(template: '/images/coupon', model: [serial: 12345])
// Render to a file
new File("coupon.jpg").withOutputStream { outputStream ->
jpegRenderingService.render(template: '/images/coupon', model: [serial: 12345])
}
If there isn't an easy way like the above example, since my information is coming from multiple domain classes should I just create <g> tags in my gsp template that pulls based on conditions needed? If that is the case.. how would I insert a variable into my gsp template from my service?
You might take a look at the grails.gsp.PageRenderer utility class. It allows you to render .gsp templates as a String:
String gspOutput = groovyPageRenderer.render(view: '<your view>.gsp', model: [ modelObj1: ... ])
... or directly to a Writer or OutputStream:
groovyPageRenderer.renderTo(view: '<your view>.gsp', model: [ modelObj1: ... ], <writer or OS>)
Much more detail can be found here: http://mrhaki.blogspot.com/2012/03/grails-goodness-render-gsp-views-and.html
To render the template as text file you should set the content type of the response to text/plain
I'd think you just create your text template as standard, with <g> tags etc..., then call the standard grails render() function on the template with a contentType of 'text/plain'. No plugin necessary?
Add the following code to the GSP file
<%#page contentType="text/plain"%>

adding attributes to data.xml using xbl component in form builder

How can we send hint of Single-Line Text component, like xml attributes in Orbeon Form Builder to data.xml?
For example:
hint: first-name="Erik" last-name="Bruchez" email="info#orbeon.com"
<?xml version="1.0" encoding="utf-8" ?>
<form>
<contact first-name="Erik" last-name="Bruchez" email="info#orbeon.com" />
</form>
Now send button generate data.xml like :
<contact>
<first-name>Erik</first-name>
<last-name>Bruchez</last-name>
<email>info#orbeon.com</email>
<phone>6505555555</phone>
</contact>
Or maybe is there some ways to define data.xml attributes from Form Builder?
It look to me like you're trying to bind a control to an attribute (/form/contact/#first-name), instead of an element (/form/contact/first-name). You can do this if you write XForms by hand, but not if you're creating the form with Form Builder. With Form Builder, the structure of the XML used to collect the data is automatically created for you by Form Builder.
If this XML needs to go to another system that expects data in a different format, then you can, on submission, implement your own service to which the data is sent, and you can do the transformation in that service, for instance using XSLT.

How do I find a <div> tag with a specific attribute value using Xerces-J?

I am using Xerces in Java. I would like to parse an HTML document to find a div element having a specific attribute (e.g., id = myID). Upon finding said element, I would like to return the text content within the div. I have been unable to find any examples of this online for Xerces.
Example:
<div id="myId">foo</div>
This should return foo.
Sorry this doesn't answer using Xerces-J, but there is a library called jsoup that is made for this sort of thing (though I'm sure Xerces can do this as well). It's sort of like Javascript for Java. Jsoup allows you to do something like this:
String html = "<div id=\"myId\">foo</div>";
Document doc = Jsoup.parse(html);
String divfoo = doc.getElementById("myId").text();
System.out.println(divfoo);
What do you think?

Grails/Groovy taglib handling parsing dynamically inserted tags

Is there a way to have a custom taglib operate on data loaded in a .gsp file such that it picks up any tags embedded in the data stored in the database. For instance, let's say I'm doing:
<g:each in="${activities}">
<li>${it.payload}</li>
</g:each>
And inside the payload, which is coming from the database, is text like
"Person a did event <company:event id="15124124">Event Description</company:event>"
Can you have a taglib that handles company:event tags on the fly?
You could write a custom tag which uses the GroovyPagesTemplateEngine to process the text and write it to the output stream. I think you can get an instance of the TemplateEngine injected into your tag from the applicationContext.
I don't have any example code sorry,
cheers
Lee

Resources