- This topic has 4 replies, 2 voices, and was last updated 18 years, 4 months ago by
David.
-
AuthorPosts
-
DavidMemberI 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?February 19, 2007 at 11:41 am #266296
Riyad KallaMemberwhat happens if you just switch your code order? It looks like you are consistently creating the *reverse* of what you want… so flip it 🙂
February 19, 2007 at 5:22 pm #266342
DavidMemberI tried that and although it was a good idea it didn’t work. Is there anyway to create a DOM that has ordered attributes – I have never heard of it. I know that I could manually create a String, but that would require restructuing massive code.
February 19, 2007 at 5:29 pm #266343
Riyad KallaMemberThis is new to me… I’ve never tried to build a DOM out like this, but for it to not be orderable for some reason seems very odd. Is the ordering different every time you run the same code?
February 19, 2007 at 5:41 pm #266345
DavidMemberThis is very old code that I have inherited, so I cannot say why it is done this way. But all I can figure is that it is going alphabetical because no matter the approach or order, it comes out this way. What is odd is that it is enforced by the host. They must not be translating it to a XML DOM, but rather string parser or something.
-
AuthorPosts