ws-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Amila Suriarachchi <amilasuriarach...@gmail.com>
Subject Re: How to get the namespace from an OMElement when prefix is known.
Date Wed, 05 Jan 2011 05:56:31 GMT
On Wed, Jan 5, 2011 at 3:57 AM, Andreas Veithen
<andreas.veithen@gmail.com>wrote:

> 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\<http://pojo.service.test.apache.org/xsd%5C>
> "
> > xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\<http://www.w3.org/2001/XMLSchema-instance%5C>
> "
> > 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.
>

thanks Andreas, I'll use this method.

thanks,
Amila.

>
> > 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
>
>


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

Mime
View raw message