使用STAX解析SOAP消息

时间:2015-12-30 07:47:19

标签: xml soap stax

我已经使用STAX将以下代码编写到Parse SOAP消息中。我在xmlStreamReader.hasNext()处获得空指针异常。请帮忙。

    Source src = soapMsg.getSOAPPart().getContent();
    ...
    HorizonHeaderData header = parser.parseHorizonHeader(src);
    ...
    public HorizonHeaderData parseHorizonHeader(Source src) {
    HorizonHeaderData header = new HorizonHeaderData();
    XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
    if (null != src) {

        try {
            XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(src);


            if(xmlStreamReader==null){
                System.out.println("xmlStreamReader is null");
            }

            while(xmlStreamReader.hasNext()){
                int event = xmlStreamReader.getEventType();
                switch(event){
                case XMLStreamConstants.START_ELEMENT:
                        System.out.println("Element: "+ xmlStreamReader.getLocalName() + "\nValue: "+ xmlStreamReader.getElementText());
                }

                xmlStreamReader.next();

输入XML是

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:bpel="http://xmlns.oracle.com/TestLoggingComposite/SSNService/BPELProcess2">
   <soapenv:Header>
    <oas:Security soapenv:mustUnderstand="0" xmlns:oas="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:soap="soap" xmlns:xsi="xsi">
         <wsu:SecurityContextToken xmlns:wsu="http://docs.oasis-open.org/ws-sx/ws-secureconversation/200512">
            <wsu:Identifier>${=java.util.UUID.randomUUID()}</wsu:Identifier>
         </wsu:SecurityContextToken>
      </oas:Security>
   </soapenv:Header>
   <soapenv:Body/>
</soapenv:Envelope>	

我可以使用DOM查看值,但使用STAX时会出现异常。请帮助。

1 个答案:

答案 0 :(得分:0)

以下是用VTD-XML做的代码......与SAX,DOM或Pull相比,它是最先进的...对于重型处理,它是首屈一指的......这里是最近的一篇题为“使用Java处理XML - 性能基准”的研究论文,供您娱乐......

http://sdiwc.net/digital-library/request.php?article=0d947fb50e2f0160a75ac9f6bbf0818a

import com.ximpleware.*;
import java.io.*;
public class ExtractSOAPHeader {
    public static void main(String[] s) throws VTDException, FileNotFoundException,IOException{
        VTDGen vg = new VTDGen();
        vg.selectLcDepth(5);// enable better random access performance
        if (!vg.parseFile("d:\\xml\\soapheader.xml", true))
            return;
        VTDNav vn = vg.getNav();
        AutoPilot ap = new AutoPilot(vn);
        ap.declareXPathNameSpace("wsu",
                "http://docs.oasis-open.org/ws-sx/ws-secureconversation/200512");
        ap.declareXPathNameSpace("soapenv", 
                "http://schemas.xmlsoap.org/soap/envelope/");
        ap.declareXPathNameSpace("oas",
                "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
        // compile XPath expression might be expensive, try to take it out of any loop and in any warmer-up section of the code
        ap.selectXPath("/soapenv:Envelope/soapenv:Header/oas:Security/wsu:SecurityContextToken/wsu:Identifier/text()");
        int i= ap.evalXPath();
        if (i!=-1)
            System.out.println(" the text node ==>"+vn.toString(i)+ "  "+i);
    }

}