Java 如何从cxf soap消息中提取元素

Java 如何从cxf soap消息中提取元素,java,web-services,spring-mvc,soap,cxf,Java,Web Services,Spring Mvc,Soap,Cxf,我有下面的soap消息。我想提取Id和chargeBoxIdentity元素 ID: 2 Address: http://localhost:8080/ocppserver/services/CentralSystemService Encoding: UTF-8 Http-Method: POST Content-Type: application/soap+xml;charset=UTF-8;action="/Authorize" Headers: {accept-encoding=[gzi

我有下面的soap消息。我想提取Id和chargeBoxIdentity元素

ID: 2
Address: http://localhost:8080/ocppserver/services/CentralSystemService
Encoding: UTF-8
Http-Method: POST
Content-Type: application/soap+xml;charset=UTF-8;action="/Authorize"
Headers: {accept-encoding=[gzip,deflate], connection=[Keep-Alive], Content-Length= [852], content-type=[application/soap+xml;charset=UTF-8;action="/Authorize"], host=[localhost:8080], user-agent=[Apache-HttpClient/4.1.1 (java 1.5)]}
Payload: <s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
  <h:chargeBoxIdentity xmlns:h="urn://Ocpp/Cs/2012/06/" xmlns="urn://Ocpp/Cs/2012/06/">REE001</h:chargeBoxIdentity>
  <a:From>
     <a:Address>http://127.0.0.1:8081/ChargeBox/Ocpp</a:Address>
  </a:From>
  <a:MessageID>urn:uuid:2ae9d933-4f92-4628-84fe-35024a858450</a:MessageID>
  <a:ReplyTo>
     <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
  </a:ReplyTo>
  <a:Action>/Authorize</a:Action></s:Header>
 <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <authorizeRequest xmlns="urn://Ocpp/Cs/2012/06/">
     <idTag>EF1234</idTag>
  </authorizeRequest>
</s:Body>
</s:Envelope>
ID:2
地址:http://localhost:8080/ocppserver/services/CentralSystemService
编码:UTF-8
Http方法:POST
内容类型:应用程序/soap+xml;字符集=UTF-8;action=“/Authorize”
标题:{accept encoding=[gzip,deflate],connection=[Keep Alive],Content Length=[852],Content type=[application/soap+xml;charset=UTF-8;action=“/Authorize”],host=[localhost:8080],user agent=[Apache-HttpClient/4.1.1(java 1.5)]]
有效载荷:
REE001
http://127.0.0.1:8081/ChargeBox/Ocpp
urn:uuid:2ae9d933-4f92-4628-84fe-35024a858450
http://www.w3.org/2005/08/addressing/anonymous
/授权
EF1234
我写在拦截器下面。拦截器下面打印标题和有效载荷字段。如何提取Id?如何从有效负载中提取chargeboxIdentity?我使用的是log4j框架

import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
import org.apache.cxf.helpers.CastUtils;
import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.io.CachedOutputStream;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.Phase;

public class InBoundMessage extends AbstractSoapInterceptor {

    public InBoundMessage() {
        super(Phase.RECEIVE);
    }

    public void handleMessage(SoapMessage message) throws Fault {
            Map<String, List<String>> headers = CastUtils.cast((Map)message.get(Message.PROTOCOL_HEADERS));
            if (headers != null) {
                 Set<Entry<String,List<String>>> hashSet=headers.entrySet();
                 for(Entry<String,List<String>> entry:hashSet ) {
                     System.out.println("Key="+entry.getKey()+", Value="+entry.getValue());
                 }
            }
            System.out.println("Message id = "+message.getId());
            try{
            InputStream is = message.getContent ( InputStream.class );
            CachedOutputStream os = new CachedOutputStream ( );
            IOUtils.copy ( is, os );
            os.flush();
            message.setContent (  InputStream.class, os.getInputStream ( ) );
            is.close();
            String payload=IOUtils.toString ( os.getInputStream ( ) );
            System.out.println ("The request is: " + payload);

            }catch(Exception e){
                e.printStackTrace();
            }
    }

}
import java.io.InputStream;
导入java.util.List;
导入java.util.Map;
导入java.util.Map.Entry;
导入java.util.Set;
导入org.apache.cxf.binding.soap.SoapMessage;
导入org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
导入org.apache.cxf.helpers.CastUtils;
导入org.apache.cxf.helpers.IOUtils;
导入org.apache.cxf.interceptor.Fault;
导入org.apache.cxf.io.CachedOutputStream;
导入org.apache.cxf.message.message;
导入org.apache.cxf.phase.phase;
公共类InBoundMessage扩展了AbstractSoapInterceptor{
公共InBoundMessage(){
超级(相位接收);
}
public void handleMessage(SoapMessage消息)引发错误{
Map headers=CastUtils.cast((Map)message.get(message.PROTOCOL_headers));
如果(标题!=null){
Set hashSet=headers.entrySet();
for(条目:hashSet){
System.out.println(“Key=“+entry.getKey()+”,Value=“+entry.getValue());
}
}
System.out.println(“Message id=“+Message.getId());
试一试{
InputStream is=message.getContent(InputStream.class);
CachedOutputStream os=new CachedOutputStream();
IOUtils.copy(is,os);
os.flush();
message.setContent(InputStream.class,os.getInputStream());
is.close();
字符串负载=IOUtils.toString(os.getInputStream());
System.out.println(“请求为:“+有效负载”);
}捕获(例外e){
e、 printStackTrace();
}
}
}

我使用SAX解析器解析soap消息

import java.io.StringReader;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class ParseInBoundSoapMessage {

    private static InBoundSoapMessage inBoundSoapMessage=null;


    public static InBoundSoapMessage getInBoundSoapMessage(String xml) {
        try {
            inBoundSoapMessage=new InBoundSoapMessage();
            SAXParserFactory factory = SAXParserFactory.newInstance();
            factory.setNamespaceAware(true);
            SAXParser saxParser = factory.newSAXParser();
            DefaultHandler handler = new DefaultHandler() {

                boolean ischargeBoxIdentityPresent = false;
                boolean isAddressPresent = false;
                boolean isFromPresent=false;
                boolean isActionPresent=false;
                boolean isMessageID=false;
                boolean isRelatesTo=false;
                public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                    if (localName.equals("chargeBoxIdentity")) {
                        ischargeBoxIdentityPresent = true;
                    }
                    if (localName.equals("From")) {
                        isFromPresent = true;
                    }
                    if (localName.equals("Address")) {
                        isAddressPresent = true;
                    }
                    if(localName.equals("Action")){
                        isActionPresent=true;
                    }
                    if(localName.equals("MessageID")){
                        isMessageID=true;
                    }
                    if(localName.equals("RelatesTo")){
                        isRelatesTo=true;
                    }

                }

                public void characters(char ch[], int start, int length) throws SAXException {
                    if (ischargeBoxIdentityPresent) {
                        inBoundSoapMessage.setChargeBoxIdentity(new String(ch, start, length));
                        ischargeBoxIdentityPresent = false;
                    }
                    if(isAddressPresent && isFromPresent){
                        inBoundSoapMessage.setAddressInFrom(new String(ch, start, length));
                        isAddressPresent = false;
                        isFromPresent=false;
                    }
                    if(isActionPresent){
                        inBoundSoapMessage.setAction(new String(ch, start, length));
                        isActionPresent=false;
                    }
                    if(isMessageID){
                        inBoundSoapMessage.setMessageId(new String(ch, start, length));
                        isMessageID=false;
                    }
                    if(isRelatesTo){
                        inBoundSoapMessage.setRelatesTo(new String(ch, start, length));
                        isRelatesTo=false;
                    }
                }
            };
            saxParser.parse(new InputSource(new StringReader(xml)), handler);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return inBoundSoapMessage;
    }
}