在AXIS2中返回自定义XML

时间:2014-02-05 09:13:41

标签: java android xml web-services axis2

我有一个带有AXIS2的web服务,它返回一个字符串。 AXIS2使用以下命令生成XML:

<ns:return>

  String....

</ns:return>

问题是......我可以修改这个XML结构吗?我需要返回一个复杂的XML,如果我把标记放在字符串中,解析器就不会将它检测为XML响应,它会像String一样检测它。

由于

1 个答案:

答案 0 :(得分:0)

我正在努力解决我在这个论坛中多次遇到的两个主要问题,我个人无法找到方法来实现许多人可能一直在寻找的结果,直到我能找到解决方案。

1。 eclipse向导生成的Web服务返回节点“return”下的值(包括复杂对象);问题是很多人正在寻找“如何摆脱这个节点”:)。这个过程有点复杂,但一旦完成,将很容易维护一个想要以他们自己的格式发送他/她的Web服务回复的格式。

2。 如何动态添加处理程序,以便实现上述第一个目标。

服务方法中的第一个:

  1. 在您的服务方法中(假设您的服务方法名称是doSomethingAndGiveResult())获取消息context =&gt;             MessageContext mc = MessageContext.getCurrentMessageContext();
  2. 请添加包org.apache.axis2.context.MessageContext

    1. 获取配置根上下文             ConfigurationContext cc = mc.getRootContext(); ;     ArrayList al = mc.getExecutionChain();

      AxisConfiguration ac =  cc.getAxisConfiguration();
      
      AxisOperation ao = mc.getAxisOperation(); 
      
    2. 您可以检查以下轴操作,但不需要实现我们的目标。

          for(int m=0; m<al.size(); m++)
          System.out.println("Excution chain -> " + String.valueOf(m) + "  "+ al.get(m).toString()); 
      
                  ArrayList<AxisOperation> copyOfAxisOperation = new ArrayList<AxisOperation>();
          // work only on the copy of the phaseOutFlow
          copyOfAxisOperation.addAll(ao.getPhasesOutFlow());
      
      1. 现在动态添加我们感兴趣的轴阶段的处理程序 -

        if(bRetVal == false)    // Note: Need to set the handler only once
        {
            Iterator itr5 = copyOfAxisOperation.iterator();
            FaltuHandler faltuhandler = new FaltuHandler();
        
        while(itr5.hasNext()){
        
        Phase aphase = (Phase) itr5.next();
        String str = aphase.toString(); 
        System.out.println("Phases from operation = " + str);
        if ("OperationOutPhase".equals(str))
        {
        
            try{
                aphase.setPhaseLast(faltuhandler);
        
                }catch (Exception somee) {              
              System.out.println("Some exception occured while adding handler");
                  somee.printStackTrace();
                }                               
          }
           }
        }
        
      2. 请注意bRetVal布尔值,你需要在某处设置,以便下次再次调用此处理程序设置代码。

        1. 假设您将从服务中返回一些对象。我在这里给出了如何使用相同的代码,以便您可以根据项目的需要和将要返回的内容进行选择。
        2. 虽然我们将在处理程序中做同样的事情;如果我们想要覆盖返回XML的值或格式(特别是解决这里给出这个写的问题)

          这需要从此处作为正确的XML表单返回并且还要序列化

          经过多次研究,我发现使用公理包更好;对于我的项目需要适合;但是你可以自由选择你的包裹。但请注意我们的服务方法,我们将返回OMElement。请添加包org.apache.axiom.om.OMElement(也是OMFactory和OMNamespace)

              OMFactory fac = OMAbstractFactory.getOMFactory();
              OMNamespace omNs = fac.createOMNamespace("", "");
              OMElement exec = fac.createOMElement("mynode", omNs);
          
                      OMElement lbtu = fac.createOMElement("givingSomeInstruction", null, mynode);
              lbtu.setText("doOpenABrowser");
              mynode.addChild(lbtu); 
          
                      .......
                      .......
                      return mynode;
                      // Your service method ends here
          

          第二 - 我们需要编写PhaltuHandler :)

          1. 在同一个包中创建一个类(即处理程序方法所在的com.something.yourpackage)并实现Handler -

            public class FaltuHandler implements Handler {
            
            ......
            }
            
          2. 覆盖所有方法并为您想要的任何内容添加代码。 我直接从我的项目空间复制了一些部分;添加/删除可能与您的项目相关的

             @Override
            

            public void cleanup(){

            }

            @覆盖  public void flowComplete(MessageContext arg0){

            }

            @覆盖 public HandlerDescription getHandlerDesc(){

             handlerDesc = new HandlerDescription();  
             handlerDesc.setHandler(this);  
             handlerDesc.setName(this.getName());  
             return handlerDesc;
            

            }

            @覆盖 public String getName(){

            return "FaltuHandler";
            

            }

            @覆盖 public参数getParameter(String arg0){

            return null;
            

            }

            @覆盖 public void init(HandlerDescription arg0){       //无需做任何事情 }

            @覆盖 public InvocationResponse invoke(MessageContext arg0)抛出AxisFault {

            //根据我们的项目特定情况,这是我们需要做一些讨厌工作的地方 //需要这部分我在这段代码中内联解释。

            SOAPEnvelope env = arg0.getEnvelope();  SOAPBody spbd =(SOAPBody)env.getBody();  System.out.println(“从处理程序打印SOAP主体:”+ spbd.toString());

            Iterator iterator = spbd.getChildElements();

            while(iterator.hasNext()){

            OMElement element =(OMElement)iterator.next();
             System.out.println(“迭代自处理程序(top子元素) - &gt;”+ element.getLocalName());

                    // This section depicts how you can override the attributes and 
                    // values that you have set from the handler;which is already
                    // set as XML elements. And just before your XML going out
                    // of your system. Suppose I have an element called myResponse
                    // then I will check the same in the following way and possibly
                    // change some of attributes
            
            
                    if ("myResponse".equals(element.getLocalName()))
                    {
                        // adding the attributes pertaining to this packet; this section of
                        // code addresses many other related queries around this topic
            
                        OMFactory fac = OMAbstractFactory.getOMFactory();
            
                        OMAttribute attr = fac.createOMAttribute("myVersion", null, "1.0");
                        element.addAttribute(attr);
            
                        attr = fac.createOMAttribute("myStatus", null, "OK");
                        element.addAttribute(attr);
            
                        attr = fac.createOMAttribute("myID", null, "1234");
                        element.addAttribute(attr);
            
            
                    }
            
                    // Following portion has been kept for understanding of how attributes
                    // can be accessed. Skip, if you don't want. According to my project 
                    // XML format I am processing here 
            
                    Iterator it1 = element.getAllAttributes();                      
                    int k = 1;
            
                    while(it1.hasNext())
                    {
                        OMAttributeImpl elem = (OMAttributeImpl) it1.next();
                        System.out.println("Attribute" + String.valueOf(k) + "  => " + elem.getLocalName() + "  Value => " + elem.getAttributeValue());
                        k++;
                    }
            
                    // Let see if we have any child node here; most important section for getting
                    // rid of "return". You need to find our where that "return" appears in your 
                    // XML and accordingly need to take action.
            
                    Iterator it2 =  element.getChildElements();     
                    int j = 1;
            
                    while(it2.hasNext())
                    {
            
                        OMElement elem1 = (OMElement) it2.next();
            
                        if ("return".equals(elem1.getLocalName()))
                        {
                            // this we need to do in order to get rid of "return" node that gets added 
                            // automatically by the axis framework which does not meet our need
            
                            elem1.discard();
                            System.out.println("after detaching the 'return' node");
            
                            // do dirty work - since our discard will remove the entire node to be 
                            // parsed that has child node and elements. So, we will build the data 
                            // again as below
            
                            OMFactory fac = OMAbstractFactory.getOMFactory();
                            OMNamespace omNs = fac.createOMNamespace("", "");
                            // remember we returned myNode from our service?
                            OMElement mynode = fac.createOMElement("My-name-Amit", omNs); 
            
                            OMElement spcode = fac.createOMElement("givingSomeInstruction", null, mynode);
                            spcode.setText("doOpenABrowser");
                            mynode.addChild(spcode);
                            System.out.println("after doing the dirty work");
                            element.addChild(mynode);
                        }
                    }
                }
                return InvocationResponse.CONTINUE;  
            

            }

          3. 这就是你实现以下目标所取得的成就: 1.如何摆脱服务中的“return”元素 2.如何自定义从eclipse向导生成的Web服务返回的返回值/ XML 3.如何动态地将处理程序添加到轴阶段

            虽然我试图删除与项目相关的所有不必要的代码;但是不能完全删除。然而,不断发表评论,以便我们需要做些什么才能实现我们开始实现的主要目标。如果不匹配,请原谅一些大括号。

            由于 阿米特