hi,

In order to add the inheritance support to POJO we need to find the Qname
from the string getting from the xsi:type.

here is an example

String xmlString = "<ns2:addPerson xmlns:ns2=\"
http://service.test.apache.org\";>\n" +
                "            <ns2:p xmlns:ns1=\"
http://pojo.service.test.apache.org/xsd\"; xmlns:xsi=\"
http://www.w3.org/2001/XMLSchema-instance\"; xsi:type=\"ns1:Employee\">\n" +
                "               <ns1:name>AAA</ns1:name>\n" +
                "
<ns1:employeeNumber>12</ns1:employeeNumber>\n" +
                "            </ns2:p>\n" +
                "         </ns2:addPerson>";

        try {
            XMLStreamReader xmlStreamReader =
StAXUtils.createXMLStreamReader(new
ByteArrayInputStream(xmlString.getBytes()));
            StAXOMBuilder stAXOMBuilder = new
StAXOMBuilder(xmlStreamReader);
            OMElement omElement = stAXOMBuilder.getDocumentElement();

            OMElement pElement = omElement.getFirstElement();
            String instanceTypeName = pElement.getAttributeValue(new QName("
http://www.w3.org/2001/XMLSchema-instance","type";));

            QName typeQName = null;
            if (instanceTypeName.indexOf(":") > -1){
                String prefix =  instanceTypeName.substring(0,
instanceTypeName.indexOf(":"));
                for (Iterator namespaceIter =
pElement.getAllDeclaredNamespaces(); namespaceIter.hasNext();){
                    OMNamespace omNamespace = (OMNamespace)
namespaceIter.next();
                    if (omNamespace.getPrefix().equals(prefix)){
                        typeQName = new QName(omNamespace.getNamespaceURI(),
instanceTypeName.substring(instanceTypeName.indexOf(":") + 1));
                    }
                }

                System.out.println("Name space ==> " +
pElement.getXMLStreamReader().getNamespaceURI(prefix));
                System.out.println("Name space from context ==> " +
pElement.getXMLStreamReader().getNamespaceContext().getNamespaceURI(prefix));
            }

            System.out.println("Qname from iterators ==>" +
instanceTypeName);
        } catch (XMLStreamException e) {
            e.printStackTrace();
        }

this gives following out put.

Name space ==> null
Name space from context ==>
Qname from iterators ==>ns1:Employee

I can get the namespace by iterating through all the namespaces. but name
spaces are not available in stream namesapce contextes.

is there are a better way to get the namesapce without iterating?

thanks,
Amila.


-- 
Amila Suriarachchi
WSO2 Inc.
blog: http://amilachinthaka.blogspot.com/

Reply via email to