facebook

Element Attribute order

  1. MyEclipse IDE
  2.  > 
  3. Off Topic
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #266022 Reply

    David
    Member

    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?

    #266296 Reply

    Riyad Kalla
    Member

    what happens if you just switch your code order? It looks like you are consistently creating the *reverse* of what you want… so flip it 🙂

    #266342 Reply

    David
    Member

    I 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.

    #266343 Reply

    Riyad Kalla
    Member

    This 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?

    #266345 Reply

    David
    Member

    This 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.

Viewing 5 posts - 1 through 5 (of 5 total)
Reply To: Element Attribute order

You must be logged in to post in the forum log in