0
votes

I want to create below XML which having xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" in "ServiceAddRQ" as well as "Service" tag. SO is it possible to create such XML ?

Required XML :

<ServiceAddRQ echoToken="DummyEchoToken" version="2013/12"
  xmlns="http://www.test.com/schemas/2005/06/messages"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.test.com/schemas/2005/06/messages ServiceAddRQ.xsd">  
        <Service xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ServiceHotel" availToken="1jMqWGjqrx25Bp60gV2Qggb3">
        </Service>
</ServiceAddRQ>

Java Generated XML : in java generated code I am getting blank xmlns of "Service" tag...

<ServiceAddRQ xmlns="http://www.test.com/schemas/2005/06/messages"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" echoToken="6BB6B47EEEF4290515103925"
    version="2013/12"
    xsi:schemaLocation="http://www.test.com/schemas/2005/06/messages HotelValuedAvailRQ.xsd">   
    <Service xmlns="" xsi:type="ServiceHotel" availToken="1/YncBXZJXY17Z/ygNiO7gcg">        
    </Service>
</ServiceAddRQ>

Java Code For same :

    static private Namespace NSSERVICE = Namespace.getNamespace("http://www.test.com/schemas/2005/06/messages");
    static private Namespace NSSCHEMA = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");

        String sXMLOut = "";
        Element barceloDS_requests= new Element("ServiceAddRQ",NSSERVICE);  
            barceloDS_requests.setAttribute("echoToken",searchParams.getSessionID().substring(0, 12)+GenTools.getSystemDateWithTime3());
            barceloDS_requests.setAttribute("version","2013/12");
            barceloDS_requests.addNamespaceDeclaration(NSSCHEMA);
            barceloDS_requests.setAttribute("schemaLocation", "http://www.test.com/schemas/2005/06/messages HotelValuedAvailRQ.xsd", NSSCHEMA);

         Namespace xsi = Namespace.getNamespace(GenTools.returnEmptyForNull(sXMLNS_XSI), "http://www.w3.org/2001/XMLSchema-instance");
         Element eleService=new Element("Service");
            eleService.setAttribute("type", "ServiceHotel", xsi);
            eleService.setAttribute("availToken",contractInfo[1]);
    barceloDS_requests.addContent(eleService);
    sXMLOut = new GetXMLOutputter().getXMLOutputter(barceloDS_requests,true);
1
For a question "how can I change my code to produce X instead of Y" you really need to show us the current code that you want to change... But note that you don't actually need the xmlns:xsi declaration in both places because the Service element automatically inherits the namespace declarations of its parent.Ian Roberts
Hi @IanRoberts , Thanks for your valuable response. You can check my java code snippet now. Even though child tag inheriting parent tag namespace why I am getting blank 'xmlns' in Service tag?Sumit S Thakur
Can you confirm which XML object model you're using here as it's definitely not DOM (as in org.w3c.dom). It looks like JDOM, is that correct?Ian Roberts
yes @IanRoberts you are absolutely correct..I am using JDOM.Sumit S Thakur

1 Answers

1
votes

The serializer is correct to add the xmlns="" because you have created a top-level element with a default namespace declaration but then added a child element that is not in a namespace. To get the output you want you need to create the Service element with the same http://www.test.com/schemas/2005/06/messages namespace as the root ServiceAddRQ. Try it more like this:

static private Namespace NSSERVICE = Namespace.getNamespace("http://www.test.com/schemas/2005/06/messages");
static private Namespace NSSCHEMA = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");

String sXMLOut = "";
Element barceloDS_requests= new Element("ServiceAddRQ",NSSERVICE);  
barceloDS_requests.setAttribute("echoToken",searchParams.getSessionID().substring(0, 12)+GenTools.getSystemDateWithTime3());
barceloDS_requests.setAttribute("version","2013/12");
barceloDS_requests.addNamespaceDeclaration(NSSCHEMA);
barceloDS_requests.setAttribute("schemaLocation", "http://www.test.com/schemas/2005/06/messages HotelValuedAvailRQ.xsd", NSSCHEMA);

// --------------
// Key change -- Create Service element in the right namespace
Element eleService=new Element("Service",NSSERVICE);
// --------------

eleService.setAttribute("type", "ServiceHotel", NSSCHEMA);
eleService.setAttribute("availToken",contractInfo[1]);
barceloDS_requests.addContent(eleService);

This will create the correct output of:

<ServiceAddRQ echoToken="DummyEchoToken" version="2013/12"
  xmlns="http://www.test.com/schemas/2005/06/messages"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.test.com/schemas/2005/06/messages ServiceAddRQ.xsd">  
    <Service xsi:type="ServiceHotel" availToken="1jMqWGjqrx25Bp60gV2Qggb3"/>
</ServiceAddRQ>

The Service element does not need its own xmlns:xsi declaration as this is inherited from the parent, but if you specifically want to add a redundant one anyway then it may be possible using eleService.addNamespaceDeclaration(NSSCHEMA) but there's no guarantee as the serializer is always free to omit redundant namespace declarations when they don't change the semantics of the result.