Specify an XML Closing tag in Java (it auto <Self-Closes/> for empty elements) -or- Syntax to contain child nodes under specified parent branches?

I am having issues trying to force the closing tags of an XML SOAP request since Java is auto-generating self-closing tags. This is causing an issue with containment and results in invalid sub-element errors. The following shows the code used to make the SOAP body in XML. There are several nodes in the "request" that have no values but the server does hold data for. Below shows the output I'm getting and what I need. Today is my first day playing with Java so please explain in super simple terms or better yet, show an example code line. I read a similarly titled post but appears to be in a different language. I appreciate any help.

Update: On the w3schools website it states:

"Empty XML Elements - An alternative syntax can be used for XML elements with no content: instead of writing a book element (with no content) like this: <book></book> It can be written like this <book /> This sort of element syntax is called self-closing."

So, another way to state my request is, how do I prevent the Java compiler from compiling self-closing XML elements to avoid the errors: "Invalid Subelement Worker_Reference-urn:com.workday/bsvc for element Get_Workers_Request (6$16639)". I need to specify where in the tree the element / child node should close as that resolves the issue when I replicate it in other languages such as VB.

//*************** This is the body code *************** ---------------------

    SOAPBody soapBody = envelope.getBody();
    SOAPElement soapBodyElem = soapBody.addChildElement("Get_Workers_Request", "wd", "urn:com.workday/bsvc");
    SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("Request_References", "wd");
    SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("Worker_Reference", "wd");
    SOAPElement soapBodyElem3 = soapBodyElem.addChildElement("ID", "wd");
    soapBodyElem3.setAttribute("wd:type", "Employee_ID");

//*************** This is what I get *************** ------------------------

                    <wd:ID wd:type="Employee_ID">123</wd:ID>


//*************** This is what I need *************** ------------------------

                    <wd:ID wd:type="Employee_ID">123</wd:ID>

Update #2:

This is a copy and paste from Microsoft VB code with the exact changes made and the results from this excerpt. The lack of containment replicates this failure.

sEnv = sEnv & "<wd:Request_References>"
sEnv = sEnv & "<wd:Worker_Reference>"
sEnv = sEnv & "<wd:ID wd:type=""Employee_ID"">" & sEmployee_ID & "</wd:ID>"
sEnv = sEnv & "</wd:Worker_Reference>"
sEnv = sEnv & "</wd:Request_References>"
'Returns value - John Smith

sEnv = sEnv & "<wd:Request_References/>"
sEnv = sEnv & "<wd:Worker_Reference/>"
sEnv = sEnv & "<wd:ID wd:type=""Employee_ID"">" & sEmployee_ID & "</wd:ID>"
 'sEnv = sEnv & "</wd:Worker_Reference>"
 'sEnv = sEnv & "</wd:Request_References>"
'Returns Error - Invalid Subelement ID-urn:com.workday/bsvc for element Get_Workers_Request (6$16639)


How do you force XML to have closing tags?

You don't.

You are misinterpreting the error. It has nothing to do with self-closing tags. <e></e> and <e/> are completely equivalent in XML. XML processors will treat each form identically. You will not get an error with one form but not the other. If one is right, they're both right. See also XML with end tag and without it

Look instead toward the actual composition of the elements you're assembling. Your error message points you in the right direction:

Invalid Subelement xxxxxx for element Get_Workers_Request (6$16639)

You're adding an xxxxxx to Get_Workers_Request where you shouldn't be. (BTW, why in the world would you block that name?) Besides the useful validation message, check the schema that defines the expected structure for Get_Workers_Request for further guidance.

Regarding update #2

In your update #2 code, the two constructed requests are not even close to being equivalent, and they do not at all support your claim that your trouble stems from the difference between <e></e> and <e/>:

  • In the one that works, wd:Request_References contains wd:Worker_Reference.
  • In the one that fails wd:Request_References precedes wd:Worker_Reference.

Even if you learn nothing else from this answer, you'll be ahead if you think, code, and debug in terms of element containment rather than the closing of tags.

Need Your Help

fgets cannot capture output from tty device

c pipe fgets

I am trying to get the RSSI value from a Option modem installed as ttyHS4 (control) and ttyHS5 (data) on a Linux board. Expected result shows up on console but the fgets just does not capture any c...

Excel - Search an exact match within a string

filter excel-formula lookup exact-match

I'm currently struggling on finding the formula that will resolve my problem.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.