How to solve SAXException: Invalid element in
Asked Answered
J

3

5

I try to get results from a webservice in the following way.

List result = new Vector();
LibrarySearchRequest request = new LibrarySearchRequest(queryString);
LibrarySearchServicePortTypeProxy proxy = 
                                new LibrarySearchServicePortTypeProxy();
LibrarySearchServicePortType port = proxy.getLibrarySearchServicePortType();
LibrarySearchResponse response = port.process(request);
librarysearch.soft.Book[] books = response.getBooks();

When I do this I get the following exception (stacktrace) :

org.xml.sax.SAXException: Invalid element in librarysearch.soft.Book - book
at org.apache.axis.encoding.ser.BeanDeserializer.onStartChild(BeanDeserializer.java:258)
at org.apache.axis.encoding.DeserializationContext.startElement(DeserializationContext.java:1035)
at org.apache.axis.message.SAX2EventRecorder.replay(SAX2EventRecorder.java:165)
at org.apache.axis.message.MessageElement.publishToHandler(MessageElement.java:1141)
at org.apache.axis.message.RPCElement.deserialize(RPCElement.java:236)
at org.apache.axis.message.RPCElement.getParams(RPCElement.java:384)
at org.apache.axis.client.Call.invoke(Call.java:2467)
at org.apache.axis.client.Call.invoke(Call.java:2366)
at org.apache.axis.client.Call.invoke(Call.java:1812)
at librarysearch.soft.LibrarySearchServiceSOAP11BindingStub.process(LibrarySearchServiceSOAP11BindingStub.java:180)
at softarch.portal.db.ws.WS_RegularDatabase.findRecords(WS_RegularDatabase.java:44)
at softarch.portal.db.test.TestWSRegularDatabase.main(TestWSRegularDatabase.java:39)

The regular database has caught an unexpected exception: ; nested exception is: 
org.xml.sax.SAXException: Invalid element in librarysearch.soft.Book - book

I read the problem could be caused by the result returned from the webservice not matching with the classes generated from the wsdl. I regenerated my webservice client but no succes. What else could be the problem?

Joab answered 18/4, 2013 at 18:11 Comment(1)
Before digging into the code, look at the raw XML being exchanged. You can use SOAPui.Reiterant
C
6

This could be related to this bug (AXIS-2758), unresolved with Axis 1.x.

This problem can appear if your client stub is not up to date with the server side (the WSDL file). You could have to re-generate it. Like with axistools:wsdl2java.

The best nowadays, provided you use at least Java 6, is maybe to use JAX-WS on the client side (JAX-WS Maven Plugin). But it could not work with old SOAP Services using RPC/Encoded... Prefer the Document/Literal style.

Crenshaw answered 19/5, 2014 at 16:31 Comment(0)
Q
2

Workaround for this problem:

Open your generated class(For this question it is librarysearch.soft.Book). See the static code block which defines the properties(name, type, etc.) of fields.

You'll something like below:

elemField.setXmlName(new javax.xml.namespace.QName("", "book"));

change it by adding namespaceURI to it (use same namespameURI which is used at setXmlType call):

elemField.setXmlName(new javax.xml.namespace.QName("http://your.namespaceuri.here", "book"));
Quadrivial answered 26/5, 2014 at 9:28 Comment(1)
The from answer below method generates code which fixes problem. But wsdl2java still generates QName("", with empty first string argument. Should I fix it?Adduction
F
1

I had the same problem and after trying the Web Service using SoapUI, I discovered two inconsistencies between the fields I was getting in the response and the fields generated by the WSDL:

1- For some reason, when I generated my structure from the WSDL, it put a space after the field name, like this:

elemField.setXmlName(new javax.xml.namespace.QName("http://namespaceuri.here", "book "));

I just removed that space and it fixed the problem.

2- In the response I was getting an additional field that was not present in my class. What I did here was to add the field to my class, and to add also in the static block, just like any other field.

Hope it helps.

Freesia answered 26/4, 2017 at 20:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.