ws-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andreas Veithen <andreas.veit...@gmail.com>
Subject Re: How to get the namespace from an OMElement when prefix is known.
Date Tue, 04 Jan 2011 22:27:46 GMT
On Tue, Jan 4, 2011 at 09:53, Amila Suriarachchi
<amilasuriarachchi@gmail.com> wrote:
> 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.

That is not the correct algorithm to resolve a QName, because the
namespace may be declared on an ancestor of the element. OMElement has
a resolveQName method to do that correctly.

> but name spaces are not available in stream namesapce contextes.

That is kind of expected because the XMLStreamReader is initially on a
START_DOCUMENT event where the namespace context is still empty.

> is there are a better way to get the namesapce without iterating?
>
> thanks,
> Amila.
>
>
> --
> Amila Suriarachchi
> WSO2 Inc.
> blog: http://amilachinthaka.blogspot.com/
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ws.apache.org
For additional commands, e-mail: dev-help@ws.apache.org


Mime
View raw message