3

I am trying to create XML using jaxb like below format, where child element has separate name space.

 <soap:Envelope xmlns:soap="http://demo.org/soap/envelope/"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <soap:Header>
           <element1 xmlns="http://childnamespacehere">
          <att1>test</att1>
          <att2>test</att2>
          </element1>       
     </soap:Header>
     <soap:Body>
         <element2 xmlns="http://childnamespacehere">
            <att1>test</att1>
            <att2>test</att2>
        </element2 >
    </soap:Body>
</soap:Envelope>

my class

  @XmlRootElement(name = "soap:Envelope", namespace = "http://schemas.xmlsoap.org/soap/envelope/")
  public class Envelope     

    private Element1 element1;   

    private Element2  element2;

    @XmlElementWrapper(name = "soap:Header")
    @XmlElement(name = "Element1", namespace = "http://childelementnamespace/")
    public void setElement1(Element1 element){ }

    @XmlElementWrapper(name = "soap:Body")
    @XmlElement(name = "Element2" , namespace = "http://childelementnamespace/")
    public void setElement2(Element2 element){ }

but i am getting xml generated like below, where child schema is at root level.

 <soap:Envelope xsi:schemaLocation="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns2="http://childelementnamespace/"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <soap:Header>
            <ns2:Element1>
                <att1>value</att1>
                <att2>value</att2>
            </ns2:Element1>
        </soap:Header>
        <soap:Body>
            <ns2:Element2>
                 <att1>value</att1>
                <att2>value</att2>
            </ns2:Element2>
        </soap:Body>
    </soap:Envelope>

i have @xmlschema defined in package-info.java

 @XmlSchema(namespace = "http://schemas.xmlsoap.org/soap/envelope/",
    xmlns = { @javax.xml.bind.annotation.XmlNs(prefix = "Element1", namespaceURI = "http://childelementnamespace"),
            @javax.xml.bind.annotation.XmlNs(prefix = "Element2", namespaceURI = "http://childelementnamespace") },
            elementFormDefault = XmlNsForm.QUALIFIED)

    package com.model;

    import javax.xml.bind.annotation.XmlNsForm;
    import javax.xml.bind.annotation.XmlSchema;

when i generate xml , name space for child elements are not getting generated , i only get namespace for root element.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
pappu_kutty
  • 2,378
  • 8
  • 49
  • 93
  • and what is your question? – hoaz May 14 '13 at 21:02
  • i need to get child namespace while generation the xml, but i am not – pappu_kutty May 14 '13 at 22:03
  • the XML you get is valid, why do you need your NS to reside in child elements? – hoaz May 14 '13 at 22:06
  • i am calling soap service, which expects xml with child node having namespace.. if i dont have those namespace for the child nodes, service throws an exception. – pappu_kutty May 14 '13 at 22:13
  • but your child node does have a namespace i can't see the problem, they are being prefixed with ns2 which inherits teh namespace declared in teh ns2 namespace at the top. – Sean F May 15 '13 at 02:50

3 Answers3

2

i have solved by adding "xmlns" attribute to objects (childnode) Element1 and Element2.

  class Elemenet1

  @XmlAttribute(name="xmlns")
  String xmlns = "http://childnamespacehere";

  public void setXmlns(String namespace){};

  public String getXmlns(){};

Output

<soap:Envelope xmlns:soap="http://demo.org/soap/envelope/"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <soap:Header>
           <element1 xmlns="http://childnamespacehere">
          <att1>test</att1>
          <att2>test</att2>
          </element1>       
     </soap:Header>
     <soap:Body>
         <element2 xmlns="http://childnamespacehere">
            <att1>test</att1>
            <att2>test</att2>
        </element2 >
    </soap:Body>
</soap:Envelope>
pappu_kutty
  • 2,378
  • 8
  • 49
  • 93
  • What do you gain from doing it this way, You are increasing complexity for no gain. – Sean F May 16 '13 at 01:43
  • 1
    the service is exposed to me, hence i cant change the service behavior. i am curious to know about the complexity here. – pappu_kutty May 17 '13 at 18:45
  • Since you're using JAXB, just add a package-info.java to your domain's package that declares the namespace you want [see here](http://stackoverflow.com/questions/7501494/what-is-jaxb-generated-package-info-java). Jaxb will automatically put the xmlns attribute on there for you, and you only have to maintain the namespace declarations from one place. – David Sep 05 '13 at 17:48
  • Hi @pappu_kutty i'm havign this exact same issue however I can't update the objects with the new 'xmlns' attribute. I wonder if there's any other way you got this work? – Yaz Jun 18 '18 at 14:54
0

In where you say what is being generated is has xmlns:ns2="http://childelementnamespace/" up the top, this is declaring the namespace and used in this fashion <ns2:Element2> using ns2 here uses tha namspace declared previously.

So what you are expecting and what you are getting are the exact same just declared in different places, the jaxB method is more correct as it is not declaring the same namespace more than once.

Sean F
  • 2,352
  • 3
  • 28
  • 40
  • 2
    i am expecting the child namespace to be part of child node , and not part of root element. basically i am trying to find out solution to get the child namespace at child node, and as you said it is not working for me. it is soap request (please refer at top of my question) where it need namespace adjacent to child-node and it doesn't require any prefix, if i don't have this namespace at child node i get error from service. – pappu_kutty May 15 '13 at 15:48
0

This is a temporary solution. This triggers major problems when you want to unmarshall a xml document. But you can use different packages for marshall and unmarshall process too.