Question

I have a web service and client that are passing around strings containing character references such as  (0x1A). These are invalid in XML 1.0 but valid in XML 1.1. Axis's XML parser is throwing exceptions because of these character references. Is there a way to force it to parse the response as XML 1.1, or to insert the XML declaration? (There currently isn't one.) I looked into using handlers, but my understanding is that they get invoked after the XML is already parsed.

Was it helpful?

Solution

Are you passing in an InputStream or Reader? If so, you could wrap the source in another class (like BufferedReader works) but use it to drop the unnecessary characters.

OTHER TIPS

I think you're going to have a pretty tough time with this. My understanding is that the WSDL 2.0 standard is built on XML 1.0. So what kind of service are you calling that describes itself with WSDL (assuming XML 1.0) and then starts sending you messages with XML 1.1 characters in it?

When you define an Axis2 service, you can define what handlers it has in the services.xml file. For example on this page they've got a service that used the org.apache.axis2.receivers.RawXMLINOutMessageReceiver...I know that's not what you're trying to do but maybe it's a place to start looking or thinking.

There is one more option - to make the parser treat the input as XML 1.1. Since Axis2 uses Woodstox parser, you can extend the WstxInputFactory and override method createPrivateConfig():

@Override
public ReaderConfig createPrivateConfig() {
    ReaderConfig config = super.createPrivateConfig();
    config.enableXml11(true);
    return config;
}

Then use that custom factory via system property: -Djavax.xml.stream.XMLInputFactory=mypackage.MyWstxInputFactory

Note that such workaround is only for cases when you absolutely cannot alter the buggy web service that produces invalid XML. It is possible to encounter side effects.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top