Hi, I want to create tree structure using web service. I have used bottom up web service technique for creation of simple arithmatic operation. I can not understand, how to build a tree using web services. Is there is way to achieve this?
A:
Use a recursive definition like :
expr = expr1 operand expr2 | node
expr1 = expr
expr2 = expr
operand = '+' | '-'
node = number
Then you cqn straightforward encode it using JSON or XML
15 + 3 - 2 becomes
{ "expr" :
{ "expr1": { "expr1":"15" "operand":"+" "expr2":"3"}
"operand": "-"
"expr2": "2"}}
or in xml
<expr>
<expr1>
<expr1>15</expr1>
<operand>+</operand>
<expr2>3</expr2>
</expr1>
<operand>+</operand>
<expr2>2</expr2>
</expr>
In the service you can then walk the tree or build the tree. I am not sure if you see the service as a consumer or a producer.
Peter Tillemans
2010-08-20 12:44:07
A:
If you implement your web service in Groovy using Axis2, you can create a builder that produces the Axis2 low level Axiom API calls:
package example
import groovy.util.BuilderSupport
import org.apache.axiom.om.OMAbstractFactory
import org.apache.axiom.om.OMElement
import org.apache.axiom.om.OMFactory
import org.apache.axiom.om.OMNamespace
class OMElementBuilder extends BuilderSupport {
OMElement result
String namespace
OMElementBuilder(namespace) {
this.namespace = namespace
}
def createNode(name) {
return createNode(name, null, null)
}
def createNode(name, value) {
return createNode(name, null, value)
}
def createNode(name, Map attributes) {
return createNode(name, attributes, null)
}
def createNode(name, Map attributes, value) {
OMFactory fact = OMAbstractFactory.getOMFactory()
OMNamespace omNs = fact.createOMNamespace(namespace, "ns")
OMElement node = fact.createOMElement(name, omNs)
if (value) {
fact.createOMText(node, value)
}
return node
}
void setParent(parent, child) {
parent.addChild(child)
}
void nodeCompleted(parent, node) {
if (!parent) {
result = node
}
}
}
Any arbitray XML structure is then easy to create as follows:
package example
import org.apache.axis2.AxisFault
import org.apache.axiom.om.OMElement
import javax.xml.namespace.QName
public class BankService {
// withdraw operation
public OMElement withdraw(OMElement requestElement) {
// Build the response
def builder = new OMElementBuilder("http://example")
builder.withdrawResponse() {
balance("1000")
additionalInfo() {
item() {
message("hello world")
}
item() {
message("hello world")
}
item() {
message("hello world")
}
item() {
message("hello world")
}
}
}
return builder.result
}
}
Mark O'Connor
2010-08-25 18:29:39