You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ws.apache.org by Amila Suriarachchi <am...@gmail.com> on 2011/01/04 09:53:17 UTC
How to get the namespace from an OMElement when prefix is known.
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/
Re: How to get the namespace from an OMElement when prefix is known.
Posted by Amila Suriarachchi <am...@gmail.com>.
On Wed, Jan 5, 2011 at 3:57 AM, Andreas Veithen
<an...@gmail.com>wrote:
> On Tue, Jan 4, 2011 at 09:53, Amila Suriarachchi
> <am...@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/
Re: How to get the namespace from an OMElement when prefix is known.
Posted by Amila Suriarachchi <am...@gmail.com>.
On Wed, Jan 5, 2011 at 3:57 AM, Andreas Veithen
<an...@gmail.com>wrote:
> On Tue, Jan 4, 2011 at 09:53, Amila Suriarachchi
> <am...@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/
Re: How to get the namespace from an OMElement when prefix is known.
Posted by Andreas Veithen <an...@gmail.com>.
On Tue, Jan 4, 2011 at 09:53, Amila Suriarachchi
<am...@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: java-dev-unsubscribe@axis.apache.org
For additional commands, e-mail: java-dev-help@axis.apache.org
Re: How to get the namespace from an OMElement when prefix is known.
Posted by Andreas Veithen <an...@gmail.com>.
On Tue, Jan 4, 2011 at 09:53, Amila Suriarachchi
<am...@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