Hi Ruchith,

setNamespaceAware to the original DocumentBuilderFactory solve the problem
partially.

Now the DOM Element reads:
<?xml version="1.0"
 encoding="UTF-8"?>
<getHearingDetailsByCaseNoWSReturn *xmlns="http://cis.com"*>
   <hearingDetailsVO *xmlns="http://vo.cis.com"*>
     <duration/>
     <applicationNo>sAppNo0</applicationNo>
   </hearingDetailsVO>
</getHearingDetailsByCaseNoWSReturn>

But the OMElement reads:
<getHearingDetailsByCaseNoWSReturn *xmlns=""*>
   <hearingDetailsVO *xmlns="http://vo.cis.com"*>
     <duration/>
     <applicationNo>sAppNo0</applicationNo>
   </hearingDetailsVO>
</getHearingDetailsByCaseNoWSReturn>

Observations:
1. After I setNamespaceAware to the original DOM DocumentBuilderFactory, the
inner element <hearingDetailsVO> gets correct namespace. But root
element <getHearingDetailsByCaseNoWSReturn> does not. The namespace of
<getHearingDetailsByCaseNoWSReturn> is lost. I created this element in the
following way:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
Document doc = dbf.newDocumentBuilder().newDocument();
Element root = doc.createElementNS("http://cis.com";,
"getHearingDetailsByCaseNoWSReturn");

I think OMElement does not include "http://cis.com"; as namespace because "
http://cis.com"; is not explicitly declared as a namespace in DOM doc. I
searched the interfaces of Document and Element, but cannot find a way to
explicitly declare a namespace.
I think I need to treat namespace as attribute in DOM. Please correct me if
I am wrong.

Thus I added one line to declare xmlns as attribute.

root.setAttribute("xmlns", "http://cis.com";).

Is there any other elegant way?


2. Do you have a good way to remove all null elements like <duration/>? The
original DOM Element also contains other null elements which contains no
value.



Regards,
Xinjun




On 1/10/07, Ruchith Fernando <[EMAIL PROTECTED]> wrote:

Hi Xinjun,

How did u create the DOM element that is to be converted to OM? If you
read it from a file you MUST make sure you the DocumentBuilderFactory
instance that you use to create the DocumentBuilder instance that
parses the xml is *namspaceAware*.

Please see here[1] for an example.

Thanks,
Ruchith

[1] http://rafb.net/p/DDc2NH37.html


On 1/10/07, Xinjun Chen <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I am trying to convert a DOM Element to OMElement using the following
toOM
> code in  org.apache.axis2.security.util.Axis2Util.
>
>  public static OMElement toOM(Element element) throws Exception {
>   try {
>    org.apache.xml.security.Init.init();
>    ByteArrayOutputStream os = new ByteArrayOutputStream();
>    XMLUtils.outputDOM (element, os, true);
>
>    ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
>    XMLStreamReader reader =
> XMLInputFactory.newInstance().createXMLStreamReader(is);
>
>    StAXOMBuilder builder = new StAXOMBuilder(reader);
>    builder.setCache(true);
>    return builder.getDocumentElement();
>   } catch (XMLStreamException e) {
>    log.fatal(e.getMessage(), e);
>    throw new Exception("Error when converting from DOM Element to OM.");
>   }
>
> There are two problems with the code:
> 1. I must add the bolded line in the toOM() method.
> 2. This method seems not deal with namespace correctly.
>
> My original DOM Element is as follows:
> <?xml version="1.0"
> encoding="UTF-8"?><getHearingDetailsByCaseNoWSReturn
> xmlns="
> http://cis.com
"><hearingDetailsVO><duration/><applicationNo>sAppNo0</applicationNo></hearingDetailsVO></getHearingDetailsByCaseNoWSReturn>
>
> But when I use the method to convert the DOM Element into OMElement, it
> reads:
> <getHearingDetailsByCaseNoWSReturn><hearingDetailsVO><duration
>
/><applicationNo>sAppNo0</applicationNo></hearingDetailsVO></getHearingDetailsByCaseNoWSReturn>
>
>
> It seems that the namespace is ignored by the Axis2Util.toOM().
>
> After I add the OMElement to a SOAPEnvelope using the following code,
every
> element is qualified with "" namespace which is unexpected.
>
> SOAPFactory fac = OMAbstractFactory.getSOAP11Factory();
> SOAPEnvelope envelope = fac.getDefaultEnvelope();
> OMElement om = Axis2Util.toOM((Element)retNode);
> envelope.getBody().addChild(om);
>
> /*******************************
> Result SOAPEnvelope
> *******************************/
> <?xml version='1.0' encoding='utf-8'?>
> <soapenv:Envelope
>  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/";>
>  <soapenv:Header />
>  <soapenv:Body>
>   <getHearingDetailsByCaseNoWSReturn xmlns="">
>    <hearingDetailsVO xmlns="">
>     <duration xmlns="" />
>     <applicationNo xmlns="">sAppNo0</applicationNo>
>    </hearingDetailsVO>
>   </getHearingDetailsByCaseNoWSReturn>
>  </soapenv:Body>
> </soapenv:Envelope>
>
>
> Could anyone tell me how can I solve the problems?
>
>
> Regards,
> Xinjun


--
www.ruchith.org
www.wso2.org

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Reply via email to