I have a DOM that I need to have an element with exactly ordered attributes, or the third party API throws an error. Here is how I am trying to do it:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();
Element cmdNode = doc.createElement(“COMMAND”);
Approach #1
cmdNode.setAttribute(“OBJECT”, command);
cmdNode.setAttribute(“ACTION”, action);
Approach #2
Attr objAttr = doc.createAttribute(“OBJECT”);
Text commandTxt = doc.createTextNode(command);
objAttr.appendChild(commandTxt);
cmdNode.setAttributeNode(objAttr);
Attr actionAttr = doc.createAttribute(“ACTION”);
Text actionTxt = doc.createTextNode(action);
actionAttr.appendChild(actionTxt);
cmdNode.setAttributeNode(actionAttr);
But both approaches create:
<COMMAND ACTION=”Add” OBJECT=”Customer”>
instead of
<COMMAND OBJECT=”Customer” ACTION=”Add”> that I really need. is there a way to order attributes?