I look today into problem “Unexpected element”. Developer use a code generation utility wsdl2java for AXIS2. Everything working fine except attachments. Service call fail with exception “Unexpected element attachment”. I found that element attachment is successfully start to processing, but fail to finished. The problem in the code:
if (reader.isStartElement() && new javax.xml.namespace.QName("http://company.com/service/model/v1", "attachment").equals(reader.getName())) { reader.next(); if (isReaderMTOMAware(reader) && java.lang.Boolean.TRUE.equals(reader.getProperty(org.apache.axiom.om.OMConstants.IS_BINARY))) { //MTOM aware reader - get the datahandler directly and put it in the object object.setAttachment((javax.activation.DataHandler) reader.getProperty(org.apache.axiom.om.OMConstants.DATA_HANDLER)); } else { if ((reader.getEventType() == javax.xml.stream.XMLStreamConstants.START_ELEMENT) && reader.getName() .equals( new javax.xml.namespace.QName(org.apache.axiom.om.impl.MTOMConstants.XOP_NAMESPACE_URI, org.apache.axiom.om.impl.MTOMConstants.XOP_INCLUDE))) { final java.lang.String id = org.apache.axiom.om.util.ElementHelper.getContentID(reader, "UTF-8"); object.setAttachment(((org.apache.axiom.soap.impl.builder.MTOMStAXSOAPModelBuilder) ((org.apache.axiom.om.impl.llom.OMStAXWrapper) reader).getBuilder()).getDataHandler(id)); reader.next(); reader.next(); } else if (reader.hasText()) { String content = reader.getText(); object.setAttachment(org.apache.axis2.databinding.utils.ConverterUtil.convertToBase64Binary(content)); } } reader.next(); } // End of if for expected property start element
We don’t use MTOM, so problem was in section
else if (reader.hasText()){
Looking into reader data I found that content of element “attachment” contains multiple text nodes, but expected only one node. It seams that parser reads base64binary not in one piece, but multiple chunks.
So instead I use next code:
else if (reader.hasText()) { String content = ""; while(reader.hasText()){ content+=reader.getText(); reader.next(); } object.setAttachment(org.apache.axis2.databinding.utils.ConverterUtil.convertToBase64Binary(content)); }
Now all working as expected.
Hi,
I use 1.4.1 also. Please, check if you apply fix in right place of code. You could use debugger to check is fix working.