java - 无法使用 SAXFilter 删除 SOAP 响应的命名空间

标签 java xml soap jaxb saxparser

我正在尝试解析以下 SOAP 响应,因为它不是有效响应我正在尝试使用以下过滤器删除其命名空间,但它遇到了错误。

响应

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" 
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
               xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <SearchFlightsResponse xmlns="ElsyArres.API">
            <SoapMessage>
                <Username>TEST</Username>
                <Password>TESTs</Password>
                <LanguageCode>EN</LanguageCode>
                <ErrorMessage />
                <ErrorCode>0</ErrorCode>
                <AppVersion>8.0.3</AppVersion>
                <Request>
                    <Departure>FDH</Departure>
                    <Destination>HAM</Destination>
                    <DepartureDate>2014-08-08</DepartureDate>
                    <ReturnDate />
                    <NumADT>1</NumADT>
                    <NumINF>0</NumINF>
                    <NumCHD>0</NumCHD>
                    <CurrencyCode>EUR</CurrencyCode>
                    <CarrierList />
                    <FareClasses />
                    <Providers />
                    <WaitForResult>true</WaitForResult>
                    <NearbyDepartures>true</NearbyDepartures>
                    <NearbyDestinations>true</NearbyDestinations>
                    <RROnly>false</RROnly>
                    <MetaSearch>true</MetaSearch>
                </Request>
                <Response>
                    <SearchFlightId>140514114543-02-43064-52</SearchFlightId>
                    <Roundtrip>false</Roundtrip>
                    <CurrencyCode>EUR</CurrencyCode>
                    <Flights>
                        <Flight>
                            <Outbound>
                                <CarName>Inter Sky</CarName>
                                <CarCode>3L</CarCode>
                                <DepName>Friedrichshafen</DepName>
                                <DepCode>FDH</DepCode>
                                <DestName>Hamburg</DestName>
                                <DestCode>HAM</DestCode>
                                <Duration>01:45</Duration>
                                <FlightNo>3L370</FlightNo>
                                <DepDateTime>2014-08-08T06:10:00</DepDateTime>
                                <ArrDateTime>2014-08-08T07:55:00</ArrDateTime>
                                <Legs>
                                    <Leg>
                                        <Sequence>0</Sequence>
                                        <FlightNo>3L370</FlightNo>
                                        <DepCode>FDH</DepCode>
                                        <DepName>Friedrichshafen</DepName>
                                        <DestCode>HAM</DestCode>
                                        <DestName>Hamburg</DestName>
                                        <DepTime>06:10</DepTime>
                                        <ArrTime>07:55</ArrTime>
                                        <CarCode>3L</CarCode>
                                        <CarName>Inter Sky</CarName>
                                        <FareClass>Economy</FareClass>
                                        <ArrDateTime>2014-08-08T07:55:00</ArrDateTime>
                                        <DepDateTime>2014-08-08T06:10:00</DepDateTime>
                                    </Leg>
                                </Legs>
                                <Taxes>0</Taxes>
                                <FareADT>6500</FareADT>
                                <FareCHD>0</FareCHD>
                                <FareINF>0</FareINF>
                                <MiscFees>6400</MiscFees>
                                <Idx>307963</Idx>
                                <FareClass>Economy</FareClass>
                                <FareType>Web</FareType>
                                <FareId>3L0</FareId>
                            </Outbound>
                            <BagFee>0</BagFee>
                            <CcFee>600</CcFee>
                            <HandlingFee>500</HandlingFee>
                            <TotalFare>12900</TotalFare>
                            <FlightId>140514114543-02-21212-2</FlightId>
                            <Link2Book>http://sample.com</Link2Book>
                            <Provider>ElsyArres</Provider>
                        </Flight>
                        <Flight>
                            <Outbound>
                                <CarName>Inter Sky</CarName>
                                <CarCode>3L</CarCode>
                                <DepName>Friedrichshafen</DepName>
                                <DepCode>FDH</DepCode>
                                <DestName>Hamburg</DestName>
                                <DestCode>HAM</DestCode>
                                <Duration>01:45</Duration>
                                <FlightNo>3L376</FlightNo>
                                <DepDateTime>2014-08-08T18:00:00</DepDateTime>
                                <ArrDateTime>2014-08-08T19:45:00</ArrDateTime>
                                <Legs>
                                    <Leg>
                                        <Sequence>0</Sequence>
                                        <FlightNo>3L376</FlightNo>
                                        <DepCode>FDH</DepCode>
                                        <DepName>Friedrichshafen</DepName>
                                        <DestCode>HAM</DestCode>
                                        <DestName>Hamburg</DestName>
                                        <DepTime>18:00</DepTime>
                                        <ArrTime>19:45</ArrTime>
                                        <CarCode>3L</CarCode>
                                        <CarName>Inter Sky</CarName>
                                        <FareClass>Economy</FareClass>
                                        <ArrDateTime>2014-08-08T19:45:00</ArrDateTime>
                                        <DepDateTime>2014-08-08T18:00:00</DepDateTime>
                                    </Leg>
                                </Legs>
                                <Taxes>0</Taxes>
                                <FareADT>10500</FareADT>
                                <FareCHD>0</FareCHD>
                                <FareINF>0</FareINF>
                                <MiscFees>6400</MiscFees>
                                <Idx>307964</Idx>
                                <FareClass>Economy</FareClass>
                                <FareType>Web</FareType>
                                <FareId>3L0</FareId>
                            </Outbound>
                            <BagFee>0</BagFee>
                            <CcFee>600</CcFee>
                            <HandlingFee>500</HandlingFee>
                            <TotalFare>16900</TotalFare>
                            <FlightId>140514114543-02-21212-3</FlightId>
                            <Link2Book>http://sample.com</Link2Book>
                            <Provider>ElsyArres</Provider>
                        </Flight>
                    </Flights>
                </Response>
            </SoapMessage>
        </SearchFlightsResponse>
    </soap:Body>
</soap:Envelope>

代码

message.writeTo(System.out); //show message details

URL endpoint = new URL("http://testv80.elsyarres.net/service.asmx");
SOAPMessage response = connection.call(message, endpoint);

connection.close();

SOAPMessage sm = response;
System.err.println("sm is:" + sm);
System.out.println("Response:");
ByteArrayOutputStream out = new ByteArrayOutputStream();
sm.writeTo(out);

System.out.println(out.toString());  //ValidSoap message
NamespaceFilter outFilter = new NamespaceFilter(null, false);
Reader in = new StringReader(out.toString());  //reading character stream
InputSource is = new InputSource(in);
SAXSource source = new SAXSource(outFilter,is); 

第 202 行 >>>

this.results = (SearchFlightsResponse) JAXB.unmarshal(source, SearchFlightsResponse.class);
System.err.println(">" + results.getSoapMessage().getUsername());
JAXBContext context = JAXBContext.newInstance(SearchFlightsResponse.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(results, System.out);

这是我找到的过滤器实现 here

命名空间过滤器

public class NamespaceFilter extends XMLFilterImpl {

    private String usedNamespaceUri;
    private boolean addNamespace;

    //State variable
    private boolean addedNamespace = false;

    public NamespaceFilter(String namespaceUri,
            boolean addNamespace) {
        super();

        if (addNamespace)
            this.usedNamespaceUri = namespaceUri;
        else 
            this.usedNamespaceUri = "";
        this.addNamespace = addNamespace;
    }



    @Override
    public void startDocument() throws SAXException {
        super.startDocument();
        if (addNamespace) {
            startControlledPrefixMapping();
        }
    }



    @Override
    public void startElement(String arg0, String arg1, String arg2,
            Attributes arg3) throws SAXException {

        super.startElement(this.usedNamespaceUri, arg1, arg2, arg3);
    }

    @Override
    public void endElement(String arg0, String arg1, String arg2)
            throws SAXException {

        super.endElement(this.usedNamespaceUri, arg1, arg2);
    }

    @Override
    public void startPrefixMapping(String prefix, String url)
            throws SAXException {


        if (addNamespace) {
            this.startControlledPrefixMapping();
        } else {
            //Remove the namespace, i.e. don´t call startPrefixMapping for parent!
        }

    }

    private void startControlledPrefixMapping() throws SAXException {

        if (this.addNamespace && !this.addedNamespace) {
            //We should add namespace since it is set and has not yet been done.
            super.startPrefixMapping("", this.usedNamespaceUri);

            //Make sure we dont do it twice
            this.addedNamespace = true;
        }
    }

}

错误

java.lang.NullPointerException: No parent for filter
    at org.xml.sax.helpers.XMLFilterImpl.setupParse(XMLFilterImpl.java:714)
    at org.xml.sax.helpers.XMLFilterImpl.parse(XMLFilterImpl.java:356)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:203)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:181)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:232)
    at javax.xml.bind.JAXB.unmarshal(JAXB.java:259)
    at com.test.retrieveFlights(test.java:202)
at com.test.App.main(App.java:17)

最佳答案

JAXB (JSR-222) 实现不应该对 SOAP 响应有任何问题。这个问题是对你在相关问题上得到的答案的回应,但那个答案不正确。

SOAPMessage 实例是 SOAP 消息的 DOM 表示。您只需向下导航到已映射到的子元素并对其进行解码。以下是我们针对您的一个相关问题得出的结论:

关于java - 无法使用 SAXFilter 删除 SOAP 响应的命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24228672/

相关文章:

android - 我想在 android Activity 底部为 "adview"横幅保留空间而不覆盖内容

java - Spring Security 集成到 Activity 目录中

java - 使用 java 进行 SOAP 服务调用时出错

java - 如何根据 HTTP header 值在 RestController 之间切换?

java - App Engine 柔性环境是否支持 Quartz?

java - 在 Android 上使用 iPOJO 启动 apache-felix 4.2.1

php - 函数执行期间 SOAP 客户端超时

java - 使用 Springfox 记录 Spring 应用程序中的 jax-rs 服务

xml - 支持 XML 的 Spring Boot REST

css - XSLT 样式表检测表格元素但不显示它们